开发者

Date difference in PHP?

开发者 https://www.devze.com 2022-12-18 04:21 出处:网络
I\'m trying to compare two dates in PHP and figure out how many days apart the dates are. I am grabbing the first date from a MySQL database and the second date is the current date.The date in the MyS

I'm trying to compare two dates in PHP and figure out how many days apart the dates are. I am grabbing the first date from a MySQL database and the second date is the current date. The date in the MySQL database looks like this "2010-01-21 15:21:46"

$da开发者_JAVA技巧te1 = date("Y-m-d", $product['hold_date'])
$date2 = date("Y-m-d");

How can I compare these two dates and simply return the amount of days?

EDIT ---------------------------------------------------------------------------------

This works:

$date1 = date(strtotime('2009-12-25 15:21:46'));
$date2 = time();

$secondsDifference = $date2 - $date1;
$days = floor($secondsDifference / 86400);

echo $days;


$secondsDifference = $date2 - $date1;

This is the seconds between date2 and date 1. You may need to reorder depending upon which one is more recent/futher in the future. This is of course if you want a positive number.

Or you can use:

$secondsDifference = abs($date2 - $date1);

You can do simple arithmetic to find out the number of days

define("SECONDS_IN_DAY", 60*60*24);
$days = floor($secondsDifference / SECONDS_IN_DAY); // rounds down
// you can use ceil() to round it up.

In order to get the MySQL date into a seconds format, use strtotime()

$time = strtotime($mysql_time);

So do this:

$date1 = strtotime($product['hold_date']);
$date2 = time();


Strtotime will turn a date/time string into a unix timestamp, the number of seconds between the given time and the unix epoch (January 1, 1970). The time function will give you the unix timestamp for now.

You can then take one date away from the other to get the number of seconds apart the days are, and divide by 86400 (60x60x24) to get the number of days that have passed.

As an alternative to converting from mysql date time to unix inside PHP, you could use the UNIX_TIMESTAMP MySQL function.

Edit: As others have said, you probably want to floor() or ceil() the resulting number of days to get an integer.


May I suggest that you use MySQL (when possible) to calculate the difference in dates? MySQL's date functions are far more superior than PHP's. Consider:

# with CURDATE() resolving to 2010-01-25, this returns -3
SELECT DATEDIFF( '2010-01-22 15:21:46', CURDATE() );

So, I imagine you are doing something like this now:

SELECT
    product_id,
    hold_date
FROM
    products
WHERE
    something = true

Simply alter it to:

SELECT
    product_id,
    hold_date,
    DATEDIFF( hold_date, CURDATE() ) as hold_date_diff_days
FROM
    products
WHERE
    something = true


$secondsPerDay = 24 * 60 * 60;
$differenceInSeconds = $date2 - $date1;
$differenceInDays = $differenceInSeconds / $secondsPerDay;


If you can convert the dates to timestamps, you can do it like this:

$numDays = floor(($timestamp2 - $timestamp1) / (24 * 60 * 60));


With PHP 5.3 look at date_diff()


$d1 = strtotime("2010-01-21 15:21:46");
$d2 = strtotime($product['hold_date']);
echo floor(($d2-$d1)/86400) ;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号