go top

Date and time manipulations with PHP

One day you may need to find out what day of the week is going to be 27th of November 2037. Well, 27th of November is probably not the day you are wondering about, and in most cases you will change that date with your birthday, but still the question is the same. What I can do is to tell you how to check the day of your birthday say 30 or 50 or 100 years from now. I will also tell you how to manipulate dates and time and this is what the second part of this tutorial will show. The thing to begin with is how to construct a random date. You have to use this function mktime(). Let's say you are born on 14th of May 1983. We create this date as follows.

<?php
echo date("jS of M Y", mktime(0, 0, 0, 5, 14, 1983)); // 14th of May 1983
?>


Maybe you know, maybe you don't, but you were born on Saturday which is the 1 day of the year and is in 19 week. Now let's see which day of the week is going to be this date, but next year.

<?php
echo date("D j m Y", mktime(0, 0, 0, 5, 14, 2009)); // Thu 14 05 2009
?>


The same way that we extracted the current year with date() function, we can extract all other elements that represent date time. Knowing this will let us easily operate with time. Suppose you want to find out the number of days between two dates. Let's say 2nd february 2008 and your next birthday.

<?php
echo date("j", (mktime(0, 0, 0, 5, 14, $nextYear) - mktime(0, 0, 0, 2, 7, 2008))); // 8
?>


So the way to calculate time is by creating appropriate mktime() function.
To find out how much time you have between 2 given "times", your code should look similar to this:

<?php
echo date("g:i a.", (mktime(15, 30, 0, 1, 1, 2007) - mktime(11, 45, 0, 1, 1, 2007))); //5:45 am.
?>


mktime() function is very helpfull doing date arithmetic, because it will automatically calculate the correct value for out-of-range input. Here are some examples:

<?php
echo date("F j Y", mktime(0, 0, 0, 1, 32, 2007)); //February 1 2007
echo date("F j Y", mktime(0, 0, 0, 13, 1, 2007)); // January 1 2008
echo date("F j Y g:i a.", mktime(25, 0, 0, 1, 1, 2007)); // January 2 2007 1:00 am.
echo date("F j Y g:i a.", mktime(0, 61, 0, 1, 1, 2007)); // January 1 2007 1:01 am.
?>


You have probably noticed how easy we can calculate dates.

<?php
//How to get the day 3 days from now:
$today = date("j");
$thisMonth = date("n");
$thisYear = date("Y");
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+3, $thisYear));

//1 week from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth, $today+7, $thisYear));

//4 months from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+4, $today, $thisYear));

//3 years, 2 months and 35 days from now:
list($today,$thisMonth,$thisYear) = explode(" ", date("j n Y"));
echo date("F j Y", mktime(0,0,0, $thisMonth+2, $today+35, $thisYear+3));
?>


This concludes today's tutorial. This was a little bit longer than the usual ones but it's because the two parts are tightly coupled and we can't separate them from one another. I hope you enjoyed our time again.

4 Comments to "Date and time manipulations with PHP"

  • tamilarsan

    tamilarsan

    October 11, 2013 at 15:14 pm

    Hi Selected date picker from and to date range 7days allowed how do this work.

  • jayson

    jayson

    December 31, 2012 at 16:35 pm

    hi admin i need help. how can i get the Year,Day,Month and Hour,Minute, and the AM/PM when you posted a comment or message? same like on this site.. when a user post a comment it will posted the Year,Day,Month and Hour,Minute, and the AM/PM how can i do that using php? please reply with my email add thanks in advance.

  • VISHNU JITH

    VISHNU JITH

    September 1, 2011 at 07:32 am

    can u please forward the creation of admin of a project

  • Sreeraj S

    Sreeraj S

    January 15, 2010 at 13:01 pm

    Information is very useful especially for beginners

Add your comment

Captcha
    • Free Scripts

      Add great new functionalities to your website with our Free Scripts collection.

      Free scripts
    • PHP Scripts

      Check our extensive collection of top-notch PHP Scripts that will enhance your website!

      Commercial PHP scripts