Posted by: phillipnb | April 28, 2012

Manipulating date fields in PHP

There may not be many software application which does not use ‘date’. In fact, even a simple form application might collect information about todays’ date or logged-in date. So, knowing about ‘date’ manipulation is essential for every php developer. PHP does have a lot of built in functions and associated options for manipulating various activities related to the date field. Let us take a look at a few examples:

Let $invoice_date = ’12/01/2012′; This is in DD/MM/YYYY format. If you want it in YYYY-MM-DD format, you can write a few lines of code like this:

$temp_date = array();
$temp_date = explode('/',$invoice_date);
$invoice_date = $temp_date[2].'-'.$temp_date[1].'-'.$temp_date[0];
echo "\n Invoice Date = ".$invoice_date;

That is one way of doing this. You can also do this by using built in functions of PHP like:

$invoice_date = '12/07/2012';
$temp1 = explode('/',$invoice_date);
$temp2 = mktime(0,0,0,$temp1[1],$temp1[0],$temp1[2]);
echo date('Y-m-d',$temp2);
echo date('m/d/Y',$temp2);

To find the date that is 2 days ahead:

$invoice_date = '12/07/2012';
$temp1 = explode('/',$invoice_date);
$temp2 = mktime(0,0,0,$temp1[1],$temp1[0],$temp1[2]);
$temp1  = date('d/m/Y',strtotime("+2 days", $temp2));
echo $temp1;

To find date that is 1 month ahead:

$invoice_date = '12/07/2012';
$temp1 = explode('/',$invoice_date);
$temp2 = mktime(0,0,0,$temp1[1],$temp1[0],$temp1[2]);
$temp1  = date('d/m/Y',strtotime("+1 Month", $temp2));
echo $temp1;

To find date that is 1 month behind:

$invoice_date = '12/07/2012';
$temp1 = explode('/',$invoice_date);
$temp2 = mktime(0,0,0,$temp1[1],$temp1[0],$temp1[2]);
$temp1  = date('d/m/Y',strtotime("-1 Month", $temp2));
echo $temp1;

To find date that is 1 year ahead

$invoice_date = '12/07/2012';
$temp1 = explode('/',$invoice_date);
$temp2 = mktime(0,0,0,$temp1[1],$temp1[0],$temp1[2]);
$temp1  = date('d/m/Y',strtotime("+1 Year", $temp2));
echo $temp1;

To find the difference between two dates,

$date1 = '02/03/2012';
$date2 = '03/03/2012';
$temp  = strtotime($date2) - strtotime($date1);
echo "\n".$temp.' seconds';

To display the first and last dates of a month as a range:

echo date('01-M-Y', strtotime($date2)).' to '.date('t-M-Y', strtotime($date1));

This should output as for e.g. 01-Sep-2012 to 30-Sep-2012

The date() in php returns a formatted string of the local date/time. The first argument of the function is the format(in string) that we require and the second argument is what you want to format(in integer). Please look into the php manual ( for various rules/options available for formats.

These are just a few examples. The application of date() is infinite, you can use it to code any date manipulation activity that you want. In this blog, i will not be able to give all the instances that date() can be used for, i have given a few examples. Hope this will be useful to get started. So, till next time happy phping.

About these ads

Leave a Reply

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

You are commenting using your 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



Get every new post delivered to your Inbox.

Join 25 other followers

%d bloggers like this: