Jump to content

time/date calculation


alohatofu

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.