alohatofu Posted April 16, 2007 Share Posted April 16, 2007 Hello, I'm having trouble getting this code to work. can someone please take a look and tell me what I'm doing wrong? I'm trying to calculate time in minutes/days of the 2 dates. <?php $dateFormat = "m/d/Y H:i:s"; $targetDate = "4/13/2007 05:14:01"; $actualDate = "4/13/2007 04:14:01"; $secondsDiff = $targetDate - $actualDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); ?> <div> <div class="caption">REMAIN</div> <div class="result"> <?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?> </div> </div> Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/ Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 first of all, here you are trying to subtract two strings from eachother, not int values: $targetDate = "4/13/2007 05:14:01"; $actualDate = "4/13/2007 04:14:01"; have a look at the date() and time() functions. you should also have a look at strtotime(). what you want to do should be fairly simple: see how this works out for ya: <?php $dateFormat = "m/d/Y H:i:s"; $targetDate = strtotime("4/13/2007 05:14:01"); $actualDate = strtotime("4/13/2007 04:14:01"); $secondsDiff = $targetDate - $actualDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); ?> <div> <div class="caption">REMAIN</div> <div class="result"> <?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?> </div> </div> Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230394 Share on other sites More sharing options...
alohatofu Posted April 16, 2007 Author Share Posted April 16, 2007 thank you! that did work but how would I have it to countdown based on the current date time do i use time() ? That didn't work for me Thank you very much for your help!! Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230418 Share on other sites More sharing options...
alohatofu Posted April 16, 2007 Author Share Posted April 16, 2007 can anyone help please? Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230582 Share on other sites More sharing options...
paul2463 Posted April 16, 2007 Share Posted April 16, 2007 if you need a timestamp for the time now then use $actualDate = strtotime("Now"); Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230584 Share on other sites More sharing options...
boo_lolly Posted April 16, 2007 Share Posted April 16, 2007 what do you mean 'count down'? do you mean a previous date has already been set, but you want the time to change dynamically because the time it is right now is constantly changing? EDIT: if that is the case, use paul's example. Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230592 Share on other sites More sharing options...
alohatofu Posted April 16, 2007 Author Share Posted April 16, 2007 Here's my code. <?php $dateFormat = "m/d/Y H:i:s"; $targetDate = strtotime($objTask->p('deadlineDate')); $actualDate = strtotime("Now"); $secondsDiff = $actualDate - $targetDate; $remainingDay = floor($secondsDiff/60/60/24); $remainingHour = floor(($secondsDiff-($remainingDay*60*60*24))/60/60); $remainingMinutes = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))/60); $remainingSeconds = floor(($secondsDiff-($remainingDay*60*60*24)-($remainingHour*60*60))-($remainingMinutes*60)); $targetDateDisplay = date($dateFormat,$targetDate); $actualDateDisplay = date($dateFormat,$actualDate); ?> <div> <div class="caption">REMAIN</div> <div class="result"> <?php echo "$remainingDay days, $remainingHour hours, $remainingMinutes minutes, $remainingSeconds seconds";?> </div> </div> The output is REMAIN 13619 days, 22 hours, 17 minutes, 46 seconds It doens't look right to me. what else do I have to do guys? the value set deadlineDate on mysql is VARCHAR do I need to change anything on that? Thank you very much! Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230788 Share on other sites More sharing options...
kenrbnsn Posted April 17, 2007 Share Posted April 17, 2007 What does <?php echo $objTask->p('deadlineDate'); ?> produce? Ken Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230914 Share on other sites More sharing options...
jitesh Posted April 17, 2007 Share Posted April 17, 2007 This is example for test $secondsDiff = getsecs($targetDate) - getsecs($actualDate); function getsecs($date){ $date_time = explode(" ",$date); $date = $date_time[0]; $time = $date_time[1]; $date_a = explode("/",$date); $time_a = explode("/",$time); return mktime($time_a[0],$time_a[1],$time_a[2],$date_a[0],$date_a[1],$date_a[2]); } Link to comment https://forums.phpfreaks.com/topic/47243-timedate-calculation/#findComment-230932 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.