PHP dates and times

time() – Returns the current time in the number of seconds

time() function returns the current time in the number of seconds, always in UTC(Coordinated Universal Time), since the Unix Epoch (January 1 1970 00:00:00 GMT)

Epoch time = Unix timestamp

The valid range of a timestamp correspond to the minimum and maximum values for a 32-bit signed integer, for example
zero = January 1 1970 00:00:00
32bit = timestamp from Dec 1902 to Jan 2037.
64bit = billion of years

mktime() – Returns the Unix timestamp corresponding to the arguments given

mktime() – Returns the Unix timestamp corresponding to the arguments given.
mktime($hour, $minute, $second, $month, $day, $year)
Arguments may be left out in order from right to left; any omitted argument will be replaced with the current value according to the local date and time.

strtotime() – Convert any string passed into a unix timestamp

strtotime() – Tries to convert any string passed into a unix timestamp. Returns a timestamp on success, FALSE otherwise

echo strtotime("now");

echo strtotime("15 February 2019");

echo strtotime("february 15 2019");

echo strtotime("+1 day");

getdate() – Get date/time information

getdate() returns an associative array containing the date information of the timestamp, or the current local time if no timestamp is given.

$today = getdate();

How to create a Unix timestamp?

Uses one of these 3 functions:

  • time()
  • mktime()
  • strtotime()


checkdate() Checks the validity of the date formed by the arguments. Returns TRUE if the date given is valid; otherwise returns FALSE.

checkdate($month, $day, $year)

echo checkdate(2,29,2019) ? 'true' : 'false';

echo checkdate(2,29,2019) ? 'true' : 'false';

Format a Unix timestamp

To format a unix timestamp uses one of the functions:


date($format, $timestamp) – Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

The timestamp is optional and defaults are the value of time(), current time

echo date('D M Y');

//the MySQL DATETIME format
echo date("Y-m-d H:i:s");

//For days
d – Represent day of the month; two digits with leading zeros (01 or 31)
D – Represent day of the week as an abbreviation (Mon to Sun)

//For months
m – Represent a month in numbers with leading zeros (01 – 12)
n – Represent a month in numbers, without leading zeros (1 – 12)
M – Represent a month in text, as an abbreviation (Jan to Dec)

//For years
y – Represent a year in two digits (08 or 14)
Y – Represent a year in four digits (2008 or 2014)

H – Represent 24-hour format of an hour with leading zeros
i – Represent minutes with leading zeros (00 – 59)
s – Represent seconds with leading zeros (00 – 59)


strftime($format, $timestamp) – Format the time and/or date according to locale settings. Month and weekday names and other language strings respect the current locale set with setlocale() function.
The timestamp is optional and defaults is the current local time.

$timestamp = time();
echo strftime("Today is %m/%d/%y",$timestamp);
//Today is 04/23/19  


setlocale() – Sets locale information.

strftime() or date()? Which is better to use?

Generally date() is more widely used than strftime(). The difference is that date() is only able to return month/day names in English, while to format dates in other languages, you should use the setlocale() and strftime().

Be careful! Starting with PHP 5.1.2, timezone must be set to work with the date() function. If not set it will give you an error or warning.

How to set PHP time zone?

Timezone is set for the location of your hosting server. There are 2 ways to set timezone in php:

  1. Set date.timezone globally in Your php.ini file
  2. Use date_default_timezone_set() in Your PHP script


This function takes one argument, the timezone identifier, like UTC or Europe/Lisbon.
Check here the list of valid identifiers is available in the List of Supported Timezones.

Displays the current page request time
//Displays the current page request time
This page was requested at <?php echo date('l, F j, Y g:ia',$_SERVER['REQUEST_TIME']);?>

Format to MySql datetime

$timestamp = time();
$mysql_datetime = strftime("%Y-%m-%d %H-%m-%S",$timestamp);
echo $mysql_datetime;  
//2019-04-24 01-04-38 

Hello there!

I hope you find this post useful!

I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

Leave a Reply

Your email address will not be published. Required fields are marked *