-1

I have the following:

$dateStart = get_post_meta( get_the_ID(), 'startdate' , true);
$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$dStart = $dateStart->format('d/m/y');true);
$dEnd = date("d/m/y");
echo $dStart .'-'.$dEnd;

This outputs 12/06/15-28/10/15 but for the life of me I can't work out how to get the difference in days between the two dates.

Any advice? I've tried a few things but they error out each time.

Thanks

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • 5
    Possible duplicate of [How to calculate the difference between two dates using PHP?](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php) – user2959229 Oct 28 '15 at 13:33

4 Answers4

1

The most secure way I found to compute the number of days between a date DateTime $a and another on DateTime $b is to rely on their timestamps at midnight:

protected static function diffDays(DateTime $a, DateTime $b)
{        
   return round(
       ($a->setTime(0, 0, 0)->getTimestamp() - $b->setTime(0, 0, 0)->getTimestamp())
       / 86400 // 60 * 60 * 24 = 86400s in 24h
   ); 
}

Work very well for me :)

Note: I use hard coded value 86400 in order to reduce useless processing time calculating 60 * 60 * 24

Pierre Lemée
  • 342
  • 3
  • 2
0

Use this(Working Example)

date_default_timezone_set("Asia/Colombo");

$dateStart = get_post_meta( get_the_ID(), 'startdate' , true);
$datetime1 = new DateTime(date('Y-m-d', $dateStart));
$datetime2 = new DateTime(date('Y-m-d', $dateStart)));

$interval = $datetime1->diff($datetime2);
$date_diff = $interval->format('%R%a days');

echo "Difference is ".$date_diff
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
-1

Use DateTime::diff - http://php.net/manual/en/datetime.diff.php

$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$dateEnd = new DateTime();
$difference = $dateStart->diff($dateEnd);

Also note that not passing the end to diff() will assume now. So this would work also, in your particular example:

$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$difference = $dateStart->diff();
Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • Sorry - no luck here with that – pee2pee Oct 28 '15 at 13:58
  • @pee2pee This is the proper way of doing it. Perhaps you could show me more of the code and elaborate on what you're trying to get from it? This will give you a timestamp of the difference, formatting options are available with $difference->format(). Perhaps read through the PHP documentation I provided? – Mikel Bitson Oct 28 '15 at 15:45
  • I copied/pasted exactly what you posted but the page ends up blank – pee2pee Oct 29 '15 at 09:36
  • @pee2pee That sounds like the problem. Considering I didn't include 100% of your code from the first post, where did you paste it? The real purpose of stack is to gain understanding, not have people write the code for you- try to understand the datetime doc page I provided a link to? – Mikel Bitson Oct 29 '15 at 12:44
  • I do try and have tried and do understand what you are trying to say but for whatever reason, its not working. I'd love to learn more but unfortunately time is against me on this piece of work – pee2pee Oct 29 '15 at 14:15
-1
$daysBefore = round(abs(strtotime(Date("l, d F Y"))-strtotime($dateStart))/86400);
$daysAfter =  round(abs(strtotime(Date("l, d F Y"))-strtotime($dateEnd))/86400);

This seemed to be the only way I could get it to work

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • Why would anyone mark the correct answer down? Strange. – pee2pee Oct 29 '15 at 09:52
  • ^ This is likely because it's incredibly difficult to read and understand, uses way too many function calls on one line, and doesn't use the (modern) industry standard for PHP dates (DateTime). – Mikel Bitson Oct 29 '15 at 12:45