Date and time manipulations with PHP

Date and time manipulations with PHP Posted in PHP | 2 Comments

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.

Advertisements
By Rz Rasel Posted in Php

One comment on “Date and time manipulations with PHP

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s