timmah1 Posted January 30, 2008 Share Posted January 30, 2008 I need to subtract two dates. What's the easiest way of doing this? I have this $b = date("M j, Y H:i:s",$row6['date']) - date("M j, Y H:i:s",$row6['duration']); Then display as this echo date("M j, Y H:i:s",$b) But it keeps showing up as Dec 31, 1969 18:00:00. $row6['date'] $row6['dauration'] both fields are datetime (0000-00-00 00:00:00) Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/ Share on other sites More sharing options...
pdkv2 Posted January 30, 2008 Share Posted January 30, 2008 If you are using MySql use DATEDIFF(date1,date2) in the select statement to delect the calculated date.! Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453195 Share on other sites More sharing options...
timmah1 Posted January 30, 2008 Author Share Posted January 30, 2008 So right now, my select statement is this: $today = date("Y-m-d H:i:s"); $items= "SELECT * FROM items WHERE duration >= '$today'"; How would I implement what you said? Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453198 Share on other sites More sharing options...
mmarif4u Posted January 30, 2008 Share Posted January 30, 2008 Try out this function OR can get more idea from http://my.php.net/datetime.if u want to use php. <?php Function DateDiff($date1,$date2) { $timedifference=$date2-$date1; $corr=date("I",$date2)-date("I",$date1); $timedifference+=$corr; return $timedifference; } $d1=mktime(2,0,0,10,28,2007); $d2=mktime(4,0,0,10,29,2007); $period=DateDiff($d1,$d2); printf("<br>%s",date("I d.m.Y H:i",$d1)); printf("<br>%u hour",$period/3600); printf("<br>%s",date("I d.m.Y H:i",$d2)); ?> To use mysql: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453205 Share on other sites More sharing options...
pdkv2 Posted January 30, 2008 Share Posted January 30, 2008 So right now, my select statement is this: $today = date("Y-m-d H:i:s"); $items= "SELECT * FROM items WHERE duration >= '$today'"; How would I implement what you said? <?php $today = date("Y-m-d H:i:s"); $items= "SELECT DATEDIFF(date1,date2) FROM items WHERE duration >= '$today'"; ?> Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453214 Share on other sites More sharing options...
timmah1 Posted January 31, 2008 Author Share Posted January 31, 2008 I have this, but it's only displaying the year $formatted_date = date("Y-m-d H:i:s", strtotime($myrow2['duration'])); $today = date("Y-m-d H:i:s"); $ends = $today-$formatted_date1; Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453989 Share on other sites More sharing options...
timmah1 Posted January 31, 2008 Author Share Posted January 31, 2008 I used one of these examples, but nothing is displaying $date1 = date("Y-m-d H:i:s"); $date2 = $myrow2['duration']; function DateDiff($date1,$date2) { $timedifference=$date2-$date1; $corr=date("I",$date2)-date("I",$date1); $timedifference+=$corr; return $timedifference; } Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-453996 Share on other sites More sharing options...
timmah1 Posted January 31, 2008 Author Share Posted January 31, 2008 ok, I got it to work, but call me dumb, but how do I get this to display like this 23 hours, 17 minutes? Right now it displays like this: Array ( [23] => hours [17] => minutes ) Here's the code function date_diff($d1, $d2){ $d1 = (is_string($d1) ? strtotime($d1) : $d1); $d2 = (is_string($d2) ? strtotime($d2) : $d2); $diff_secs = abs($d1 - $d2); $base_year = min(date("Y", $d1), date("Y", $d2)); $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year); return array( date("H", $diff) => "hours", (int) date("i", $diff) => "minutes", ); } $a = date_diff("$d1", "$d2"); print_r($a); Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-454006 Share on other sites More sharing options...
kenrbnsn Posted January 31, 2008 Share Posted January 31, 2008 With that array, you could do: <?php $tmp = array(); foreach ($a as $num => $what) $tmp[] = $num . ' ' . $what; echo implode(', ',$tmp); ?> Ken Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-454022 Share on other sites More sharing options...
timmah1 Posted January 31, 2008 Author Share Posted January 31, 2008 you lost me on that Ken. Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-454030 Share on other sites More sharing options...
timmah1 Posted January 31, 2008 Author Share Posted January 31, 2008 Nevermind, works now. Thanks Ken Link to comment https://forums.phpfreaks.com/topic/88526-solved-date-subraction/#findComment-454038 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.