I have a function that creates time intervals between two time marks. The function works but I'm struggling to upgrade from strtotime() and use the DateTime class.
Below is a patch of code I wrote without getting errors
$timestart = new DateTime("14:00:00");
$timestop = new DateTime("20:00:00");
$date_diff = $timestop->diff($timestart);
$time_diff = $date_diff->format('%H');
Next is the entire code untouched. I get DateInterval could not be converted to int erros using the code above. Please kindly advise how to correctly implement the class.
Live example: http://codepad.org/jSFUxAnp
function timemarks()
{
//times are actually retrieved from db
$timestart = strtotime("14:00:00");
$timestop = strtotime("20:00:00");
$time_diff = $timestop - $timestart; //time difference
//if time difference equals negative value, it means that $timestop ends second day
if ($time_diff <= 0)
{
$timestop = strtotime( '+1 day' , strtotime( $row->timestop ) ); //add 1 day and change the timetsamp
$time_diff = $timestop - $timestart;
}
//create interval
$split = 3;
$interval = $time_diff/$split;
//get all timemarks
$half_interval = $interval / 2;
$mid = $timestart + $half_interval;
for ( $i = 1; $i < $split; $i ++) {
//round intervals
$round_mid = round($mid / (15 * 60)) * (15 * 60);
$result .= date('H:i:s', $round_mid) . ", ";
$mid += $interval;
}
$round_mid = round($mid / (15 * 60)) * (15 * 60);
$result .= date('H:i:s', $round_mid);
return $result;
}
outputs 15:00:00, 17:00:00, 19:00:00
Actually you're using DateTime, these are just aliases for creating DateTime instances
The equivalent would look like this:
$timestart = new DateTime("14:00:00");
$timestop = new DateTime("20:00:00");
$date_diff = $timestop->diff($timestart);
$time_diff = $date_diff->format('%H');
So this has to work, I tested it and I got correct results!
Related
i have two different break time
default break time
extra break time
here i want to sum of two times and display 12 hrs format
EX :
$default_time = "00:30";
$extra_time = "00:25";
my expected output : 00:55
but now display 01:00
this is my code
$default_time = $work_data->break_time;
$break_time = $work_data->extra_time;
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",strtotime($total_break));
Here is the function you can calculate total time by passing the arguments to functions.
$hours, $min are supposed variable which is zero
$default_time = "00:30";
$break_time = "00:25";
function calculate_total_time() {
$i = 0;
foreach(func_get_args() as $time) {
sscanf($time, '%d:%d', $hour, $min);
$i += $hour * 60 + $min;
}
if( $h = floor($i / 60) ) {
$i %= 60;
}
return sprintf('%02d:%02d', $h, $i);
}
// use example
echo calculate_total_time($default_time, $break_time); # 00:55
There is one function call to strtotime function too much.
You should leave out the strtotime() call in the last line, as $total_break already is a UNIX timestamp:
$total_break = strtotime($default_time)+strtotime($break_time);
echo date("h:i",$total_break);
The problem is that you're trying to add too specific timestamps, but what you're trying to achieve is adding two durations. So you need to convert those timestamps into durations. For that you need a base, which in your case is 00:00.
$base = strtotime("00:00");
$default_time = $work_data->break_time;
$default_timestamp = strtotime($default_time);
$default_duration = $default_timestamp - $base; // Duration in seconds
$break_time = $work_data->extra_time;
$break_timestamp = strtotime($break_time);
$break_duration = $break_timestamp - $base; // Duration in seconds
$total_break = $default_duration + $break_duration; // 55 min in seconds
// If you want to calculate the timestamp 00:55, just add the base back to it
echo date("H:i", $base + $total_break);
Consider using standard DateTime and DateInterval classes. All you will need is to convert your second variable value to interval_spec format (see http://php.net/manual/en/dateinterval.construct.php for details):
$defaultTime = "00:30";
$breakTime = "PT00H25M"; // Or just 'PT25M'
$totalBreak = (new DateTime($defaultTime))->add($breakTime);
echo $totalBreak->format('H:i');
You could try the following code fragment:
$time1 = explode(":", $default_time);
$time2 = explode(":", $break_time);
$fulltime = ($time1[0] + $time2[0]) * 60 + $time1[1] + $time2[1];
echo (int)($fulltime / 60) . ":" . ($fulltime % 60);
<?php
$time = "00:30";
$time2 = "00:25";
$secs = strtotime($time2)-strtotime("00:00:00");
$result = date("H:i:s",strtotime($time)+$secs);
print_r($result);
?>
Use below code you will definitely get your answers.
$default_time = "00:30:00";
$extra_time = "00:25:00";
$secs = strtotime($extra_time)-strtotime("00:00:00");
$result = date("H:i:s A",strtotime($default_time)+$secs);
echo $result;die;
You can modify above code as per your need.
You could try the following:
$default_time = $work_data->break_time;
$date_start = new DateTime($default_time);
$break_time = $work_data->extra_time;
$interval = new DateInterval("PT" . str_replace(":", "H", $break_time) . "M");
$date_end = $date_start->add($interval);
echo $date_end->format("H:i");
Note that this doesn't account for times which span a 24 hour period
Is there any function equivalent to DateTime::diff() in PHP 5.2?
My local server is PHP 5.3 and using DateTime::diff(). then I found that my live site uses PHP 5.2 and gives an error.
Fatal error: Call to undefined method DateTime::diff() in /var/www/some/other/dir/web/daikon/modules/projects/views/admin/log/admin_log_list.php on line 40
The PHP code:
foreach ($logs as $key => $list){
...
// show date in European way dd-mm-yyyy not in MySQL way yyyy-mm-dd
$newdate =new DateTime($list['date']) ;
echo "<td class=\"left\" width=\"8%\">".$newdate->format('d-m-Y')."</td>\n";
$starttime = new DateTime($list['start_time']);
echo "<td width=\"7%\">".date_format($starttime, 'H:i')."</td>\n";
$finishtime = new DateTime($list['finish_time']);
echo "<td width=\"8%\">".date_format($finishtime, 'H:i')."</td>\n";
$timediff = 0;
$interval = $starttime->diff($finishtime);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$timediff = $hours * 60 + $minutes;
Spudley's answer doesn't work for me--subtracting any DateTime from another gives 0 on my system.
I was able to get it to work by using DateTime::format with the 'U' specifier (seconds since Unix epoch):
$start = new DateTime('2010-10-12');
$end = new DateTime();
$days = round(($end->format('U') - $start->format('U')) / (60*60*24));
This works on both my dev system (5.3.4) and my deployment system (5.2.11).
I just needed that ( unfortunately ) for a WordPress plugin. This I use the function in 2 times:
In my class calling ->diff() ( my class extends DateTime, so $this is the reference DateTime )
function diff ($secondDate){
$firstDateTimeStamp = $this->format("U");
$secondDateTimeStamp = $secondDate->format("U");
$rv = ($secondDateTimeStamp - $firstDateTimeStamp);
$di = new DateInterval($rv);
return $di;
}
Then I recreated a fake DateInterval class ( because DateInterval is only valid in PHP >= 5.3 ) as follows:
Class DateInterval {
/* Properties */
public $y = 0;
public $m = 0;
public $d = 0;
public $h = 0;
public $i = 0;
public $s = 0;
/* Methods */
public function __construct ( $time_to_convert /** in seconds */) {
$FULL_YEAR = 60*60*24*365.25;
$FULL_MONTH = 60*60*24*(365.25/12);
$FULL_DAY = 60*60*24;
$FULL_HOUR = 60*60;
$FULL_MINUTE = 60;
$FULL_SECOND = 1;
// $time_to_convert = 176559;
$seconds = 0;
$minutes = 0;
$hours = 0;
$days = 0;
$months = 0;
$years = 0;
while($time_to_convert >= $FULL_YEAR) {
$years ++;
$time_to_convert = $time_to_convert - $FULL_YEAR;
}
while($time_to_convert >= $FULL_MONTH) {
$months ++;
$time_to_convert = $time_to_convert - $FULL_MONTH;
}
while($time_to_convert >= $FULL_DAY) {
$days ++;
$time_to_convert = $time_to_convert - $FULL_DAY;
}
while($time_to_convert >= $FULL_HOUR) {
$hours++;
$time_to_convert = $time_to_convert - $FULL_HOUR;
}
while($time_to_convert >= $FULL_MINUTE) {
$minutes++;
$time_to_convert = $time_to_convert - $FULL_MINUTE;
}
$seconds = $time_to_convert; // remaining seconds
$this->y = $years;
$this->m = $months;
$this->d = $days;
$this->h = $hours;
$this->i = $minutes;
$this->s = $seconds;
}
}
Hope that helps somebody.
I use this, seems to work alright - obviously you can add a second parameter to make it more flexible:
function GetDateDiffFromNow($originalDate)
{
$unixOriginalDate = strtotime($originalDate);
$unixNowDate = strtotime('now');
$difference = $unixNowDate - $unixOriginalDate ;
$days = (int)($difference / 86400);
$hours = (int)($difference / 3600);
$minutes = (int)($difference / 60);
$seconds = $difference;
// now do what you want with this now and return ...
}
I was trying to improve Christopher Pickslay's answer.
I made this function that returns and object with most of the properties from the original DateInterval object.
There is no "days" property, because it seems to be some bug in my test server (it always return 6015) .
Also, I am assuming every month has 30 days, which is definitely not precise, but may help.
function dateTimeDiff($date1, $date2) {
$alt_diff = new stdClass();
$alt_diff->y = floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24*365));
$alt_diff->m = floor((floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365))/30);
$alt_diff->d = floor(floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365) - ($alt_diff->m * 30));
$alt_diff->h = floor( floor(abs($date1->format('U') - $date2->format('U')) / (60*60)) - ($alt_diff->y * 365*24) - ($alt_diff->m * 30 * 24 ) - ($alt_diff->d * 24) );
$alt_diff->i = floor( floor(abs($date1->format('U') - $date2->format('U')) / (60)) - ($alt_diff->y * 365*24*60) - ($alt_diff->m * 30 * 24 *60) - ($alt_diff->d * 24 * 60) - ($alt_diff->h * 60) );
$alt_diff->s = floor( floor(abs($date1->format('U') - $date2->format('U'))) - ($alt_diff->y * 365*24*60*60) - ($alt_diff->m * 30 * 24 *60*60) - ($alt_diff->d * 24 * 60*60) - ($alt_diff->h * 60*60) - ($alt_diff->i * 60) );
$alt_diff->invert = (($date1->format('U') - $date2->format('U')) > 0)? 0 : 1 ;
return $alt_diff;
}
Yes, it's annoying that feature didn't make it into PHP5.2.
I'll assume you can't upgrade to 5.3? You should look into it; there's very little reason not to upgrade; but I'll assume you can't for whatever the reason.
First tip: If you only need a diff of less than 24hours, you can simply subtract the two time stamps, and do $time_diff = date('H:i:s',$subtracted_value);
If you're doing more than 24 hour diffs, but you're okay with just returning the number of days along with the time difference, you can expand on the above technique by doing a modulus calculation on the subtrated value, against the number of seconds in a day (ie 24*60*60, which is 86400)
$subtracted_value = $date1 - $date2;
$days_diff = $subtracted_value % 86400;
$time_diff = date('H:i:s',$subtracted_value);
If you need weeks, you can of course do $days_diff % 7.
Unfortunately, the manual technique breaks down after weeks, because months and years are variable in length (technically days are too, given daylight saving, but you can probably ignore that, especially as you're only ever going to be one hour out, at the most), but hopefully that's good enough to get you started.
Read the contrib notes in the PHP date_diff page
http://us3.php.net/manual/en/function.date-diff.php
Spudley was close, but you need to use gmdate not date.
So this works for 24 hours or less (if it's a positive value at least):
$difference = $date2->format('U') - $date1->format('U');
$time_diff = gmdate('H:i:s',$difference);
PHP has methods for working with Unix timestamps.
As has been noted by others, by working with seconds since the Unix date, it is easy to calculate times.
PHP's strtotime() converts a date to a timestamp:
$diff = round((strtotime($list['start']) - strtotime($list['finish'])) / 86400);
If you wish to calculate till the current time, time() provides the timestamp of "now":
$diff = round((time() - strtotime($list['date'])) / 86400);
86400 is the number of seconds in a day.
If you wish to convert to years use 31557000, which is almost exactly 365.24219 * 86400.
An added advantage here is that strtotime can take the input date in almost any human readable format, so it is very easy to work with within the code.
Please observe that if your DateTime object was created from a date string without any timezone information (as in '2012-01-01 05:00:00' like from mysql), then setting the timezone later with DateTimeZone objects via setTimeZone() does not change the DateTime objects internal timestamp.
$localtime = new DateTime('2012-01-01 08:00:00+02:00'); // Europe/Copenhagen (+ daylight savings)
$localtime->setTimezone(new DateTimeZone('UTC')); // convert local time to utc
$utctime = new DateTime('2012-01-01 06:00:00'); // timezone assumed utc, but is in fact unknown
$utctime->setTimezone(new DateTimeZone('UTC')); // trying to rectify missing timezone which fails, because the internal timestamp isn't modified, although any other format than 'U' may indicate so
#$utctime = new DateTime('2012-01-01 06:00:00+00:00'); // timezone stated, this works
$diff = intval($localtime->format('U')) - intval($utctime->format('U'));
echo $diff; // expecting zero
Here is the best solution for your question.
<?php
/* Find difference between two dates in days. PHP 5.2.x. */
$d1 = date('Y-m-d', strtotime('2013-06-13'));
$d2 = date("Y-m-d");
echo diff($d1, $d2);
function diff($date1, $date2) {
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24) / (60*60*24));
return $days;
}
?>
I been trying to fix this php percentage calculator of the day...basically right now here is about 230pm and I am getting 73% of the day completed....it should be more like 60% of the day. Here is the code:
$now = time();
$today = strtotime(date("m/d/Y"));
$seconds = $now - $today;
$day = 24*60*60;
$percent = $seconds / $day*100;
I attempted to write my own version but I am getting 100% of the day...Here is the code:
$todaysTime = time();
$todaysStart = time()-86400;
$todayCalc = $todaysTime - $todaysStart;
$dayPhpOne = 24*60*60;
$percentDay = $todayCalc / $dayPhpOne*100;
It is done in php where am I messing up my code?
Try this:
$percentDay = time() % 86400 / 864;
Edit
From the comments I take, that I didn't elaborate on time zones. Let me make clear, that this is meant to be UTC day percent.
This solution does respect timezone and other time-related complexities:
$d = new DateTime();
$h = $d->format('H');
$m = $d->format('i');
$s = $d->format('s');
$currentSecond = $h * 3600 + $m * 60 + $s;
$midnight = new DateTime($d->format('Y-m-d'), $d->getTimezone());
$tomorrow = clone $midnight;
$tomorrow = $tomorrow->add(new DateInterval('P1D'));
$secondsToday = $tomorrow->getTimestamp() - $midnight->getTimestamp();
$percent = $currentSecond / $secondsToday * 100;
var_dump($percent);
If necessary - it's possible to specify a particular timezone to be used as a second DateTime constructor argument.
I can't wrap my brain around this one so I hope someone can help. I have a song track that has the song length in milliseconds. I also have the date the song played in DATETIME format. What I am trying to do is find out how many milliseconds is left in the song play time.
Example
$tracktime = 219238;
$dateplayed = '2011-01-17 11:01:44';
$starttime = strtotime($dateplayed);
I am using the following to determine time left but it does not seem correct.
$curtime = time();
$timeleft = $starttime+round($tracktime/1000)-$curtime;
Any help would be greatly appreciated.
For my needs I used the following approach:
$curTime = microtime(true);
// something time consuming here
...
// get time difference in milliseconds
$timeConsumed = round(microtime(true) - $curTime,3)*1000;
So, the point is that we use float representation of time here (see http://php.net/manual/en/function.microtime.php)
Hope you will adopt it for your needs.
i use the following set of functions for handling mysql dates, maybe they can help you:
function sqlArray($date, $trim=true) {
$result = array();
$result['day'] = ($trim==true) ? ltrim(substr($date,8,2),'0') : substr($date,8,2);
$result['month'] = ($trim==true) ? ltrim(substr($date,5,2),'0') : substr($date,5,2);
$result['year'] = substr($date,0,4);
$result['hour'] = substr($date,11,2);
$result['minutes'] = substr($date,14,2);
return $result;
}
function sqlInt($date) {
$date = sqlArray($date);
return mktime($date['hour'], $date['minutes'], 0, $date['month'], $date['day'], $date['year']);
}
function difference($dateStart, $dateEnd) {
$start = sqlInt($dateStart);
$end = sqlInt($dateEnd);
$difference = $end - $start;
$result = array();
$result['ms'] = $difference;
$result['hours'] = $difference/3600;
$result['minutes'] = $difference/60;
$result['days'] = $difference/86400;
return $result;
}
in your case it should be something like:
$dateplayed = '2011-01-17 11:01:44';
print_r(difference($dateplayed, date('Y:m:d')));
hope it works :D
I have written this function to calculate duration between given two timestamps (with milliseconds).
function calculateTransactionDuration($startDate, $endDate)
{
$startDateFormat = new DateTime($startDate);
$EndDateFormat = new DateTime($endDate);
// the difference through one million to get micro seconds
$uDiff = ($startDateFormat->format('u') - $EndDateFormat->format('u')) / (1000 * 1000);
$diff = $startDateFormat->diff($EndDateFormat);
$s = (int) $diff->format('%s') - $uDiff;
$i = (int) ($diff->format('%i')) * 60; // convert minutes into seconds
$h = (int) ($diff->format('%h')) * 60 * 60; // convert hours into seconds
return sprintf('%.6f', abs($h + $i + $s)); // return total duration in seconds
}
$startDate = '02-Mar-16 07.22.13.000548';
$endDate = '02-Mar-16 07.22.14.000072';
$difference = calculateTransactionDuration($startDate, $endDate);
//Outputs 0.999524 seconds
You could convert the datetime string/input into unixtimestamp and then get the difference. If you do have milliseconds, unixtimestamp would have digits after the decimal. Once you have the difference, you can convert that value back into your date time pattern using function date in php. Below is the link.
Good luck!
http://php.net/manual/en/function.date.php
I used this function for my self:
public function calculateStringTimeToMiliseconds($timeInString)
{
$startTime = new \DateTime("now");
$endDate = new \DateTime($timeInString);
$interval = $startTime->diff($endDate);
$totalMiliseconds = 0;
$totalMiliseconds += $interval->m * 2630000000;
$totalMiliseconds += $interval->d * 86400000;
$totalMiliseconds += $interval->h * 3600000;
$totalMiliseconds += $interval->i * 60000;
$totalMiliseconds += $interval->s * 1000;
return $totalMiliseconds;
}
Is there any function equivalent to DateTime::diff() in PHP 5.2?
My local server is PHP 5.3 and using DateTime::diff(). then I found that my live site uses PHP 5.2 and gives an error.
Fatal error: Call to undefined method DateTime::diff() in /var/www/some/other/dir/web/daikon/modules/projects/views/admin/log/admin_log_list.php on line 40
The PHP code:
foreach ($logs as $key => $list){
...
// show date in European way dd-mm-yyyy not in MySQL way yyyy-mm-dd
$newdate =new DateTime($list['date']) ;
echo "<td class=\"left\" width=\"8%\">".$newdate->format('d-m-Y')."</td>\n";
$starttime = new DateTime($list['start_time']);
echo "<td width=\"7%\">".date_format($starttime, 'H:i')."</td>\n";
$finishtime = new DateTime($list['finish_time']);
echo "<td width=\"8%\">".date_format($finishtime, 'H:i')."</td>\n";
$timediff = 0;
$interval = $starttime->diff($finishtime);
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$timediff = $hours * 60 + $minutes;
Spudley's answer doesn't work for me--subtracting any DateTime from another gives 0 on my system.
I was able to get it to work by using DateTime::format with the 'U' specifier (seconds since Unix epoch):
$start = new DateTime('2010-10-12');
$end = new DateTime();
$days = round(($end->format('U') - $start->format('U')) / (60*60*24));
This works on both my dev system (5.3.4) and my deployment system (5.2.11).
I just needed that ( unfortunately ) for a WordPress plugin. This I use the function in 2 times:
In my class calling ->diff() ( my class extends DateTime, so $this is the reference DateTime )
function diff ($secondDate){
$firstDateTimeStamp = $this->format("U");
$secondDateTimeStamp = $secondDate->format("U");
$rv = ($secondDateTimeStamp - $firstDateTimeStamp);
$di = new DateInterval($rv);
return $di;
}
Then I recreated a fake DateInterval class ( because DateInterval is only valid in PHP >= 5.3 ) as follows:
Class DateInterval {
/* Properties */
public $y = 0;
public $m = 0;
public $d = 0;
public $h = 0;
public $i = 0;
public $s = 0;
/* Methods */
public function __construct ( $time_to_convert /** in seconds */) {
$FULL_YEAR = 60*60*24*365.25;
$FULL_MONTH = 60*60*24*(365.25/12);
$FULL_DAY = 60*60*24;
$FULL_HOUR = 60*60;
$FULL_MINUTE = 60;
$FULL_SECOND = 1;
// $time_to_convert = 176559;
$seconds = 0;
$minutes = 0;
$hours = 0;
$days = 0;
$months = 0;
$years = 0;
while($time_to_convert >= $FULL_YEAR) {
$years ++;
$time_to_convert = $time_to_convert - $FULL_YEAR;
}
while($time_to_convert >= $FULL_MONTH) {
$months ++;
$time_to_convert = $time_to_convert - $FULL_MONTH;
}
while($time_to_convert >= $FULL_DAY) {
$days ++;
$time_to_convert = $time_to_convert - $FULL_DAY;
}
while($time_to_convert >= $FULL_HOUR) {
$hours++;
$time_to_convert = $time_to_convert - $FULL_HOUR;
}
while($time_to_convert >= $FULL_MINUTE) {
$minutes++;
$time_to_convert = $time_to_convert - $FULL_MINUTE;
}
$seconds = $time_to_convert; // remaining seconds
$this->y = $years;
$this->m = $months;
$this->d = $days;
$this->h = $hours;
$this->i = $minutes;
$this->s = $seconds;
}
}
Hope that helps somebody.
I use this, seems to work alright - obviously you can add a second parameter to make it more flexible:
function GetDateDiffFromNow($originalDate)
{
$unixOriginalDate = strtotime($originalDate);
$unixNowDate = strtotime('now');
$difference = $unixNowDate - $unixOriginalDate ;
$days = (int)($difference / 86400);
$hours = (int)($difference / 3600);
$minutes = (int)($difference / 60);
$seconds = $difference;
// now do what you want with this now and return ...
}
I was trying to improve Christopher Pickslay's answer.
I made this function that returns and object with most of the properties from the original DateInterval object.
There is no "days" property, because it seems to be some bug in my test server (it always return 6015) .
Also, I am assuming every month has 30 days, which is definitely not precise, but may help.
function dateTimeDiff($date1, $date2) {
$alt_diff = new stdClass();
$alt_diff->y = floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24*365));
$alt_diff->m = floor((floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365))/30);
$alt_diff->d = floor(floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365) - ($alt_diff->m * 30));
$alt_diff->h = floor( floor(abs($date1->format('U') - $date2->format('U')) / (60*60)) - ($alt_diff->y * 365*24) - ($alt_diff->m * 30 * 24 ) - ($alt_diff->d * 24) );
$alt_diff->i = floor( floor(abs($date1->format('U') - $date2->format('U')) / (60)) - ($alt_diff->y * 365*24*60) - ($alt_diff->m * 30 * 24 *60) - ($alt_diff->d * 24 * 60) - ($alt_diff->h * 60) );
$alt_diff->s = floor( floor(abs($date1->format('U') - $date2->format('U'))) - ($alt_diff->y * 365*24*60*60) - ($alt_diff->m * 30 * 24 *60*60) - ($alt_diff->d * 24 * 60*60) - ($alt_diff->h * 60*60) - ($alt_diff->i * 60) );
$alt_diff->invert = (($date1->format('U') - $date2->format('U')) > 0)? 0 : 1 ;
return $alt_diff;
}
Yes, it's annoying that feature didn't make it into PHP5.2.
I'll assume you can't upgrade to 5.3? You should look into it; there's very little reason not to upgrade; but I'll assume you can't for whatever the reason.
First tip: If you only need a diff of less than 24hours, you can simply subtract the two time stamps, and do $time_diff = date('H:i:s',$subtracted_value);
If you're doing more than 24 hour diffs, but you're okay with just returning the number of days along with the time difference, you can expand on the above technique by doing a modulus calculation on the subtrated value, against the number of seconds in a day (ie 24*60*60, which is 86400)
$subtracted_value = $date1 - $date2;
$days_diff = $subtracted_value % 86400;
$time_diff = date('H:i:s',$subtracted_value);
If you need weeks, you can of course do $days_diff % 7.
Unfortunately, the manual technique breaks down after weeks, because months and years are variable in length (technically days are too, given daylight saving, but you can probably ignore that, especially as you're only ever going to be one hour out, at the most), but hopefully that's good enough to get you started.
Read the contrib notes in the PHP date_diff page
http://us3.php.net/manual/en/function.date-diff.php
Spudley was close, but you need to use gmdate not date.
So this works for 24 hours or less (if it's a positive value at least):
$difference = $date2->format('U') - $date1->format('U');
$time_diff = gmdate('H:i:s',$difference);
PHP has methods for working with Unix timestamps.
As has been noted by others, by working with seconds since the Unix date, it is easy to calculate times.
PHP's strtotime() converts a date to a timestamp:
$diff = round((strtotime($list['start']) - strtotime($list['finish'])) / 86400);
If you wish to calculate till the current time, time() provides the timestamp of "now":
$diff = round((time() - strtotime($list['date'])) / 86400);
86400 is the number of seconds in a day.
If you wish to convert to years use 31557000, which is almost exactly 365.24219 * 86400.
An added advantage here is that strtotime can take the input date in almost any human readable format, so it is very easy to work with within the code.
Please observe that if your DateTime object was created from a date string without any timezone information (as in '2012-01-01 05:00:00' like from mysql), then setting the timezone later with DateTimeZone objects via setTimeZone() does not change the DateTime objects internal timestamp.
$localtime = new DateTime('2012-01-01 08:00:00+02:00'); // Europe/Copenhagen (+ daylight savings)
$localtime->setTimezone(new DateTimeZone('UTC')); // convert local time to utc
$utctime = new DateTime('2012-01-01 06:00:00'); // timezone assumed utc, but is in fact unknown
$utctime->setTimezone(new DateTimeZone('UTC')); // trying to rectify missing timezone which fails, because the internal timestamp isn't modified, although any other format than 'U' may indicate so
#$utctime = new DateTime('2012-01-01 06:00:00+00:00'); // timezone stated, this works
$diff = intval($localtime->format('U')) - intval($utctime->format('U'));
echo $diff; // expecting zero
Here is the best solution for your question.
<?php
/* Find difference between two dates in days. PHP 5.2.x. */
$d1 = date('Y-m-d', strtotime('2013-06-13'));
$d2 = date("Y-m-d");
echo diff($d1, $d2);
function diff($date1, $date2) {
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24) / (60*60*24));
return $days;
}
?>