Rabu, 01 Juli 2015

Contoh Mktime php part 1 (copas )

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:
1
2
3
4
$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:
1
2
3
4
5
$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:
1
2
3
4
5
$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:
1
2
3
4
5
$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:
1
2
3
4
5
$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
1
2
3
4
5
$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,
1
2
3
4
$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:
1
echo date('01-M-Y', strtotime($date2)).' to '.date('t-M-Y', strtotime($date1));

source
https://phillipnb.wordpress.com/2012/04/28/manipulating-date-fields-in-php/

Tidak ada komentar:

Posting Komentar