3

Say I have the following 2 dates, a start date and end date:

             Year-Month-Day Hours:Minutes:Seconds  

Start Date:  2010-12-03 14:04:41
Expiry Date: 2010-12-06 12:59:59

How could I, using PHP subtract the two dates and be left with something like:

Difference: -3 days, 2 minutes and 18 seconds (If expiry date is past 3 days for example).

2 Answers 2

6

http://www.php.net/manual/en/datetime.diff.php

<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');
//result will be +2 days
?>

I hope that is what you are looking for.

Sign up to request clarification or add additional context in comments.

3 Comments

This works if all you want is days, but won't provide the details that the OP asked for.
echo $interval->format('%R%d days, %i minutes and %s seconds.');
Thanks a million lves, My php version doesn't support this code yet, but it is really elegant and easy to read!
4

This is based on numerous online examples; you'll see similar code all around if you get your google on.

function timeSince($dateFrom, $dateTo) {
    // array of time period chunks
    $chunks = array(
        array(60 * 60 * 24 * 365 , 'year'),
        array(60 * 60 * 24 * 30 , 'month'),
        array(60 * 60 * 24 * 7, 'week'),
        array(60 * 60 * 24 , 'day'),
        array(60 * 60 , 'hour'),
        array(60 , 'minute'),
    );

    $original = strtotime($dateFrom);
    $now      = strtotime($dateTo);
    $since    = $now - $original;
    $message  = ($now < $original) ? '-' : null;

    // If the difference is less than 60, we will show the seconds difference as well
    if ($since < 60) {
        $chunks[] = array(1 , 'second');
    }

    // $j saves performing the count function each time around the loop
    for ($i = 0, $j = count($chunks); $i < $j; $i++) {

        $seconds = $chunks[$i][0];
        $name = $chunks[$i][1];

        // finding the biggest chunk (if the chunk fits, break)
        if (($count = floor($since / $seconds)) != 0) {
            break;
        }
    }

    $print = ($count == 1) ? '1 ' . $name : $count . ' ' . $name . 's';

    if ($i + 1 < $j) {
        // now getting the second item
        $seconds2 = $chunks[$i + 1][0];
        $name2 = $chunks[$i + 1][1];

        // add second item if it's greater than 0
        if (($count2 = floor(($since - ($seconds * $count)) / $seconds2)) != 0) {
            $print .= ($count2 == 1) ? ', 1 ' . $name2 : ', ' . $count2 . ' ' . $name2 . 's';
        }
    }
    return $message . $print;
}

It was intended to show the difference between a given time and the current time, but I've made slight changes to show the difference between two times instead. You may wish to change the output from a postfix of ' ago' to a prefix of 'Difference: '.

7 Comments

Thanks El Yobo: unfortunately I get a fatal error: Fatal error: Cannot redeclare timesince() (previously declared in /usr/local/pem/vhosts/103503/webspace/httpdocs/clients/wp-content/themes/discount-codes/page-ending-soon.php:34) in /usr/local/pem/vhosts/103503/webspace/httpdocs/clients/wp-content/themes/discount-codes/page-ending-soon.php on line 34
Ha, it looks like you either already have a function with the same name (in which case just rename my function to something else) or you're including the file with the function in it more than once (in which case, use include_once instead of include).
My bad, was in a loop :) - Works almost perfect! Just one thing, All results return "ago". For example: "1 week ago". Even though that 1 week ago should be 1 week in the future?
Glad to help :) As I said, not all my code, various online examples assisted when I wrote it, e.g. viralpatel.net/blogs/2009/06/…
in $message = ($now > $original) ? '-' : null; it should be "<" instead. It's normal that current time is more than original, shouldn't produce negative time.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.