PHP
ProgrammingTips & Tricks

Date/Time in PHP

Here are some of the examples on how you could get the current date or time in PHP. We will walkthrough the basic commands or codes where we can set the timezone in PHP and get the current date and time based on the current timezone.

Timezones

You can see what is the timezone your system or server is using by using this below:

echo date_default_timezone_get();
// returns the system's timezone

If you would like to change the timezone in your server or system, you can use this code below:

date_default_timezone_set('America/Los_Angeles');
// this will set the system's timezone to America, Los Angeles

Also, note that using the command “date_default_timezone_set” to change the timezone is temporary only. If you’re setting it in one of the PHP files, it won’t affect the other PHP files. Meaning, if you try to get the timezone in other part of your project, it will returns the default system timezone.

If you want to change the timezone permanently, you will need to change the timezone settings in php.ini file and restart the PHP or http service. Refer to the screenshot below.

Look for date.timezone in your php.ini file. Set the desired timezone for your system.

Here is the list of supported timezone in PHP. https://www.php.net/manual/en/timezones.php

How to get the current Date/Time

You can set your own date/time format like below.

$date = date('m/d/Y h:i:s a', time());
echo $date; 
// returns "11/06/2022 08:28:34 am"
$date = date('Y-m-d H:i:s');
echo $date;
// returns "2022-11-06 08:31:33"

Here are more examples,

$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
$today = date("H:i:s");                         // 17:16:18
$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (the MySQL DATETIME format)

Here’s another way of getting the current date time with PHP class.

$now = new DateTime();
echo $now->format('Y-m-d H:i:s');  
echo $now->getTimestamp();          
$now = new DateTime(null, new DateTimeZone('America/New_York')); 
$now->setTimezone(new DateTimeZone('Europe/London'));
echo $now->getTimezone();

Using the time() function will return the current UNIX time, example below. Every second will return a different number.

echo time();
// returns something like "1667723635"

Date Time Compare

You can compare the date time in PHP using the comparison operators with datetime object.

$today = new DateTime("now");
$tomorrow = new DateTime("tomorrow");
var_dump($today == $tomorrow); // returns false
var_dump($today < $tomorrow);  // returns true
var_dump($today > $tomorrow);  // returns false

Another example,

$date = date('Y-m-d H:i:s');
$date2 = date('Y-m-d H:i:s', strtotime('2 day'));
if($date2 > $date){
    echo "true";
} else {
    echo "false";
}
// returns true

Adding or Subtracting Date/Time

There are several ways to add the number of hours, days, etc in the current date/time. Below are few examples,

date('Y-m-d H:i:s', strtotime('4 minute'));
date('Y-m-d H:i:s', strtotime('6 hour'));
date('Y-m-d H:i:s', strtotime('-2 day')); // subtracts 2 days from current date
date('Y-m-d H:i:s', strtotime('2 day'));

You can also add using seconds, for example below, adding one hour to the time “8:30”.

$timestamp = strtotime('08:30') + 60*60;
$time = date('H:i', $timestamp);
echo $time; // return 09:30