Showing posts with label PHP Date Time. Show all posts
Showing posts with label PHP Date Time. Show all posts

Thursday, September 22, 2011

php time arithmetic and time difference between to times


Working with time different is very interesting in PHP, here is a function to find out the time difference and give the output in time format either it can be a 12hr/ 24hrs format. let us write this function to find out the
time arithmetic and time difference between to times using PHP. This function is very useful for compare the duration between two times, such employee work entry system, total hours of work


We can find the difference between Two time eg: 10:00:00 AM to 01:00:00 PM or 10:00:00 AM to 13:00:00 PM.


<?php
# Desc: function to return Time difference in between the two Times in a day
# Parm: Start_time end Time as 12hr/24hr format within single day
# Return: duration in time format: hh:mm:ss
function time_difference($start_time="11:00:00",$end_time="12:00:00")
{
list($h1,$m1,$s1)    =    split(':',$start_time);  
$startTimeStamp    =    mktime($h1,$m1,$s1,0,0,0);

list($h2,$m2,$s2)    =    split(':',$end_time);   

//check end time is in 12 hrs format:
if($h2 < $h1)
$h2+=12;

$endTimeStamp    =    mktime($h2,$m2,$s2,0,0,0); 
$time=abs($endTimeStamp - $startTimeStamp);

$value = array(
"hours" => "00",
"minutes" => "00",
"seconds" => "00"

);

if($time >= 3600){
$value["hours"] = sprintf("%02d",floor($time/3600));
$time = ($time%3600);
}
if($time >= 60){
$value["minutes"] = sprintf("%02d",floor($time/60));
$time = ($time%60);
}

$value["seconds"] = sprintf("%02d",floor($time));

return implode(":",$value); 
}

echo time_difference("11:45:00","01:30:00");
echo "<br />";
echo time_difference("13:00:00","15:00:00");

//Output:
// 01:45:00
// 02:00:00

?>

Wednesday, September 14, 2011

Get yesterday date PHP

Here is the different way of Getting yesterday date with PHP
using strtotime() and mktime()  we can find the previous date in easy way.

# Using strtotime() function
// from Current Date
echo date("d-m-Y",strtotime("yesterday"));

# Using mktime() function
// from Specific date  eg: Oct-01-2011
echo date("d-m-Y",mktime(0,0,0,10,1,2011)-1);


Output:
13-09-2011
30-09-2011



Get First and Last date of the Month from given date with php

When working with date ranges we need to find out the month starting and end dates from the given date. here is the PHP function to Get First and Last date of the Month from given date with php

functions used:
list(), split(), date(), mktime(), array()




<?php

# Desc: Funcion to get First and Last date of the Month from Given Date
# Parm: $anyDate ( as YYYY-MM-DD format), $return Format (eg: d-m-Y );
# Return: Array with Two Values of First and Last Date

function first_last_date_of_month($anyDate,$returnFormat)
{
 // separate year, month and date
 list($yr,$mn,$dt)    =    split('-',$anyDate);    
 //Create time stamp of the first day from the give date.
 $timeStamp            =    mktime(0,0,0,$mn,1,$yr);
 //get first day of the given month
 $firstDay            =     date($returnFormat,$timeStamp);    
 //Find the last date of the month and separating it
 list($y,$m,$t)        =     split('-',date('Y-m-t',$timeStamp)); 
 //create time stamp of the last date of the give month
 $lastDayTimeStamp    =    mktime(0,0,0,$m,$t,$y);
 // Find last day of the month
 $lastDay            =    date($returnFormat,$lastDayTimeStamp);
 // return the result in an array format.
 $arrDay                =    array("$firstDay","$lastDay"); 
 return $arrDay;
}

//Usage
$month_range=array();
$month_range=first_last_date_of_month('2011-09-10','d-m-Y');
print $month_range[0];
echo "<br>";
print $month_range[1];

?>