简体   繁体   中英

PHP Get days difference between a date and the date today (Now)

I can't seem to get this to work. I have tried from the samples online but there wasn't one the is exactly what I needed. Basically I want to be able to display the number of days that passed from the given date. My sample below is a combined HTML and PHP, I had to do it this way for some reasons.

<?php
$OldDate = strtotime($row['DateSigned']);
$NewDate = date('M j, Y', $OldDate);
?>

<b>Date Signed:</b> <?php echo $NewDate; ?>
<b>Days Since Signed:</b> <?php echo date_diff(strtotime($NewDate),Date("y/m/d")); ?>

This seem to fail. Date("y/m/d") is the date today. Can you tell me what went wrong?

This will work:

<?php
$OldDate = strtotime("2015-10-21");
$NewDate = date('M j, Y', $OldDate);
$diff = date_diff(date_create($NewDate),date_create(date("M j, Y")));
?>

<b>Date Signed:</b> <?php echo $NewDate; ?>
<b>Days Since Signed:</b> <?php echo $diff->format('%R%a days'); ?>

using date_diff, it expects a DateTime object rather than an integer. Here is an example to get you where you may want to be

<?php
    $OldDate = new DateTime('2009-10-11');
    $now = new DateTime(Date('Y-m-d'));
    print_r($OldDate->diff($now));
?>

This outputs (as of the day of this post) ::

[y] => 6
[m] => 0
[d] => 14
[h] => 0
[i] => 0
[s] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 2205
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0

See DateTime::diff

Thanks everyone, I found a simpler solution (Simpler for beginner like me to understand) :)

$now = time(); // or your date as well
$your_date = strtotime($NewDate);
$datediff = ceil(($now - $your_date)/86400);

$datediff is now showing the number of days.

It's too late to reply and there are other good answer but I would like to share what worked for me.

date_default_timezone_set("Asia/Karachi");
$old_date = new DateTime('2018-12-01 04:10:58');
$now = new DateTime(Date('Y-m-d'));
$interval = $old_date->diff($now);

echo $interval->d.' days<br>';

// you can also get years, month, hours, minutes, and seconds
echo $interval->y.' years<br>';
echo $interval->m.' months<br>';
echo $interval->h.' hours<br>';
echo $interval->i.' minutes<br>';
echo $interval->s.' seconds<br>';

Below is code that will get you the date difference with current date, i hope it helps.

    $date = $row['DateSigned'];
    $diff = date_diff(date_create($date), date_create(date('Y-m-d')));
    echo $diff->format("%a");

The result you will get is number of days.

Try to use timestamps.

$from = mktime(0,0,0,6,1,2015); // example old date
$to = time(); // now

echo round(($to - $from)/86400); // gives you elapsed days

86400 is the # of seconds in a day.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM