PHP dates and times

PHP dates and times
5 (100%) 1 vote

time()

time() function returns the current time in the number of seconds 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()

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()

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

echo strtotime("now");
//1551919987

echo strtotime("15 February 2019");
//1550185200

echo strtotime("february 15 2019");
//1550185200


echo strtotime("+1 day");
//1552006448

How to create a Unix timestamp?

Uses one of these 3 functions:

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

Checkdate()

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';
//false

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

Leave a Reply

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