Page 1 of 1

Help with subtracting datetime??

Posted: Sat Nov 12, 2011 5:16 pm
by tech0925
How can I pull in the current datetime and subtract if from my $enddate and put that into a variable that would echo something like 89 days 12 hours 22 minutes and 33 seconds left? I really don't care to have the seconds listed though.


Here is my current code:

Code: Select all

$enddate = "2012-02-12 22:20:24";  
$current_date_time=date("Y-m-d h:i:s"); 

echo $current_date_time; 

$now = new DateTime("$enddate"); 
$ref = new DateTime("$current_date_time"); 
$diff = $now->diff($ref); 
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);  

When I echo $current_date_time it seems to be the right time except the hours part. It echos 05 when it should be 17 for military time. Now, when I print the final part it says 0 days, 17 hours, 17 minutes which the days part cant be right. What am I missing and can I put this into a variable instead of using printf?

Re: Help with subtracting datetime??

Posted: Sat Nov 12, 2011 7:02 pm
by Vegan
I think PHP uses 24 hour times but for some reason you code does not seem to roll over properly

Re: Help with subtracting datetime??

Posted: Sat Nov 12, 2011 7:13 pm
by Celauran
I can't duplicate the problem.

Re: Help with subtracting datetime??

Posted: Sat Nov 12, 2011 7:27 pm
by tech0925
Thank you so much for taking the time to help! I changed my code to this:

Code: Select all

//$now = new DateTime(); // current date/time 
    $current_date_time=date("Y-m-d h:i:s"); 

    echo $current_date_time; 

    $now = strtotime($enddate); 
    $ref = strtotime($current_date_time); 
    $diff = $now-$ref; 
    echo "<br />$diff"; 



When I echo $diff it looks like this: 7988419 Not to sure how to format that and if that is correct. Everytime I refresh the page the last two numbers change so I am assuming that is seconds. I would like to format this to where it only shows how many days, hours, and minutes are left. Any idea how I can do that? Again, thanks for your time!

Re: Help with subtracting datetime??

Posted: Sat Nov 12, 2011 8:11 pm
by tech0925
Quick Update:

I was able to figure it out by doing the following:

Code: Select all

//$now = new DateTime(); // current date/time
    $current_date_time=date("Y-m-d H:i:s");


    $now = strtotime($enddate);
    $ref = strtotime($current_date_time);
    $diff = $now-$ref;
    $diffminutes = $diff/60;
    $diffminutes = round($diffminutes);
    $diffhours = $diffminutes/60;
    $diffhours = round($diffhours);
    $diffdays = $diffhours/24;
    $diffdays = round($diffdays);
Maybe that will help someone else as well :D Thanks again 4 all your responses!

Re: Help with subtracting datetime??

Posted: Sun Nov 13, 2011 1:02 pm
by McInfo
In your original code, the two problems were:
  1. A lowercase "h" in a date format string makes the hour display in 12-hour format with leading zeros. So, when given $current_date_time, the DateTime constructor interpreted afternoon (PM) times as morning (AM) times because it assumed it was being given the hour in 24-hour format.
  2. $diff->d is the number of days remaining after calculating the number of years ($diff->y) and months ($diff->m). To get the total number of days, use $diff->days.

Code: Select all

<?php
$now  = new DateTime('now');
$then = new DateTime('2012-02-12 22:20:24');
$diff = $now->diff($then);
printf(
    "Between %s and %s are %d days, %d hours, %d minutes, and %d seconds.\n",
    $now->format('Y-m-d H:i:s'), $then->format('Y-m-d H:i:s'),
    $diff->days, $diff->h, $diff->i, $diff->s
);
To solve the problem mathematically, first divide the total seconds into days. A day has 86400 seconds. Divide the remaining seconds into hours (3600 seconds/hour); then do minutes (60 seconds/minute).

Code: Select all

<?php
define('HOURS_PER_DAY', 24);
define('MINUTES_PER_HOUR', 60);
define('SECONDS_PER_MINUTE', 60);
define('SECONDS_PER_HOUR', SECONDS_PER_MINUTE * MINUTES_PER_HOUR); // 3600
define('SECONDS_PER_DAY', SECONDS_PER_HOUR * HOURS_PER_DAY); // 86400

$now = time();
$then = strtotime('2012-02-12 22:20:24');

$seconds = abs($now - $then);
$days = floor($seconds / SECONDS_PER_DAY);
$seconds %= SECONDS_PER_DAY; // Equivalent to { $seconds -= ($days * SECONDS_PER_DAY); }
$hours = floor($seconds / SECONDS_PER_HOUR);
$seconds %= SECONDS_PER_HOUR;
$minutes = floor($seconds / SECONDS_PER_MINUTE);
$seconds %= SECONDS_PER_MINUTE;

echo "$days days, $hours hours, $minutes minutes, $seconds seconds\n";

Re: Help with subtracting datetime??

Posted: Sun Nov 13, 2011 1:28 pm
by tech0925
Thanks McInfo!! One question, is my way ok or should I be doing it the way you have it? Thanks again!

Re: Help with subtracting datetime??

Posted: Sun Nov 13, 2011 4:42 pm
by McInfo
I don't think the last solution you posted gives the result you want. It gives totals, not an accumulation of different units. That can be remedied by taking the remainder (modulus division) as shown in the code I posted here and in my last post.

Also, rounding can introduce extra time. For example, rounding a duration of 90 seconds (1.5 minutes) yields 2 minutes instead of 1 minute. Therefore, the quotients must be truncated with floor(). The floor() function rounds away from zero for negative numbers, which can cause off-by-one errors; so the duration should be a positive number. Get the absolute value of the time difference with abs().

You can, however, work from the bottom up (minutes, hours, days) as you did.

Code: Select all

<?php
define('SECONDS_PER_MINUTE', 60);
define('MINUTES_PER_HOUR', 60);
define('HOURS_PER_DAY', 24);

$now = time();
$then = strtotime('2012-02-12 22:20:24');

$seconds = abs($now - $then);

$minutes = floor($seconds / SECONDS_PER_MINUTE);
$seconds %= SECONDS_PER_MINUTE;
$hours = floor($minutes / MINUTES_PER_HOUR);
$minutes %= MINUTES_PER_HOUR;
$days = floor($hours / HOURS_PER_DAY);
$hours %= HOURS_PER_DAY;

echo "$days days, $hours hours, $minutes minutes, $seconds seconds\n";
Given that it takes the least code and it is closest to the code you started this thread with, I would suggest the first solution I posted (DateTime). But use whatever you are comfortable with, as long as it works.

Re: Help with subtracting datetime??

Posted: Sun Nov 13, 2011 5:20 pm
by tech0925
Ok, thank you so much!