PHP Date and Time Reference

PHP Date and Time Reference

Let me admit here I am not very good at remembering stuff. PHP like every other maintained programming language has like thousands if not millions of function names and parameters.
I cannot remember all the functions and classes for a specific task. I simply needed an online reference for PHP date time functions so that I dont have to go snoop around like 30 pages to find one itsy bitsy function.

date() month pretty much handy while you are dealing with date/time functionality in PHP. for example

print date("d-m-Y");
Output:  05-07-2016

this function would print current date in simple day-month-year format as shown above.

Output:  10:37:58

and this function would print time in mundane hour:minutes:seconds format as given below

You can also add other details like day and am/pm as well.

print date("l d-m-Y h:i A");
Output:  Tuesday 05-07-2016 10:44 AM

If you wanted to check if a date exists or not, you can use checkdate(‘m’,’d’,’Y’) function
It works something like this

$check = (checkdate(20, 10, 1985)) ? "TRUE" : "FALSE";
print $check; 
Output:  FALSE

It would print false as output because 20th doesnt exist. or does it?

There’s a date_create() function to create an date object so date/time funcsionality.

$date = date_create();
Output:  object(DateTime)[1]
public 'date' => string '2016-07-02 09:06:20' (length=19)
public 'timezone_type' => int 3
public 'timezone' => string 'Europe/Paris' (length=12)

date_diff() is a function that gives out the interval between two given dates.

$date1 = date_create("2013-03-15");
$date2 = date_create("2013-12-12");
$diff = date_diff($date1,$date2);
var_dump($diff) ;
Output:  object(DateInterval)[3]
public 'y' => int 0
public 'm' => int 8
public 'd' => int 27
public 'h' => int 0
public 'i' => int 0
public 's' => int 0
public 'weekday' => int 0
public 'weekday_behavior' => int 0
public 'first_last_day_of' => int 0
public 'invert' => int 0
public 'days' => int 272
public 'special_type' => int 0
public 'special_amount' => int 0
public 'have_weekday_relative' => int 0
public 'have_special_relative' => int 0

date_format() is a function to format date as provided.

print date_format($date,"d-m-Y");
Output:  30-07-1999

time() function gives you current epoch or unix time.

print time();
Output:  1467707548

You can format the output in human reabible using gmdate() function

$unixTime = time();
print gmdate('l h:i:s d-m-Y', $unixTime);
Output:  Tuesday 10:09:13 05-07-2016

function returns relative time,string unix timestamp format.

print strtotime("now");
print strtotime("12 December 2009");
print strtotime("+8 hours");
print strtotime("+1 week");
print strtotime("+1 week 3 days 7 hours 5 seconds");
print strtotime("next Friday");


Output:  1260572400

Relative Time
Here’s one script I have found on github to find relative time.

Just copy above above function and then you can use it in your program like

print time2str(strtotime("10-june-2016"));

And the output would be something like this

Output:  4 weeks ago

I’ll keep adding date/time functions in this post. If you see anything worng or want me to append anything to this post, @wiqi is my twitter handle and your suggestions are always more than welcome.



I am a web developer based in Lahore. I spend my days playing with wordpress, code igniter, wide variety of key web projects and frameworks.


  1. roulette casino : November 9, 2017 at 5:35 am

    Woah! I’m really loving the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and visual appeal.
    I must say that you’ve done a superb job with this.

    Additionally, the blog loads super fast for me on Firefox.

    Outstanding Blog!

Leave a Reply

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

thirteen + twenty =

Translate »