current date in php – How to get the current Date and Time in PHP?

current date in php : Using date() Function & Using DateTime() Class to get current Date & Time in PHP. The main focus of the post is to get the current Date and Time in PHP.

current date in php – How to Get Current Date and Time in PHP?

Solution PHP date: Use the PHP date() Function with Set the new timezone.

Using date() Function

Example : 1

<?php
    $created_at = date('Y-m-d H:i:s');
    echo $created_at;
?>

Results

2022-02-25 08:22:26

More examples

  date('Y/m/d');           //  2022/02/25
  date('m/d/Y');           //  02/25/2022
  date('H:i:s');           //  08:22:26
  date('Y-m-d H:i:s');     //  2022-02-25 08:22:26

Using DateTime() Class:

Example

<?php
    $created_at = new DateTime();
    echo $created_at->format('Y-m-d H:i:s');
?>

Results

2022-02-25 08:22:26

don’t Miss : Get current time in PHP

Read Also:  Laravel update multiple rows with different values

How to Get the Current Date and Time in PHP?

Set the new timezone

<?php
date_default_timezone_set('Asia/Kolkata');
$created_at = date('d-m-y h:i:s');
echo $created_at;
?>

Return current date from the remote server

$created_at = date('d-m-y h:i:s');
echo $created_at;
?>

I hope you get an idea about current date in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.