3

Possible Duplicate:
How many days until XXX date?

Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?

 $timezone = "Asia/Dhaka";
 if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);

 $now = time();
 $last_date = strtotime("$year-$month-$day");
 $datediff = $last_date - $now;
 $day_left=floor($datediff/(60*60*24));
 echo "$day_left day(s) left.";

N:B: My timezone is +6 GMT, I mean Asia/Dhaka.

Community
  • 1
  • 1
Shahriar Kabir
  • 274
  • 1
  • 9
  • 26
  • There are some similar questions. As I want to edit my own code, I posted my question here. Thanks. – Shahriar Kabir Dec 25 '12 at 01:03
  • 1
    @rambocoder that is an old post using a now archaic method of determing the days. We now have DateTime() using the diff() method to achieve it using a modern approach. – kittycat Dec 25 '12 at 01:27

1 Answers1

9

As per the PHP documentation:

<?php

$year = '2012';
$month = '12';
$day = '26';

$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');

?>
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • I printed your code, but it showed +6015 days, I think it should be 2 days. – Shahriar Kabir Dec 25 '12 at 01:14
  • @user1902462 prints 2 days for me. See edited code, I had previously posted the PHP.net example verbatim. Btw you would need to edit it to input your variables. – kittycat Dec 25 '12 at 01:14
  • I appreciate your reply. But it couldn't work in my localhost. It says 6015 day for every time I tried. :( Anyway I accepted your answer. Thanks a lot. – Shahriar Kabir Dec 25 '12 at 01:29
  • @user1902462 did you copy the most current post exactly? I did it so all you have to do is basically copy and paste (remove the temp $year, $month and $day vars of course) – kittycat Dec 25 '12 at 01:32
  • Yes I did and in a whole new page. But it is 6015 day(s). – Shahriar Kabir Dec 25 '12 at 01:37
  • @user1902462 What version of PHP are you running? – kittycat Dec 25 '12 at 01:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/21661/discussion-between-cryptic-and-user1902462) – kittycat Dec 25 '12 at 01:40
  • How do you display the word "day" if $interval is only 1? I've tried the following: if ($interval > '1') { echo $interval->format('%a days'); } else { echo $interval->format('%a day'); } but this still displays "days" even when 1 is displayed. – NathonJones Oct 28 '16 at 08:32
  • @NathonJones `$interval` is not a string, it's an object. So do this: `$days = $interval->format('%a'); echo "$days day"; if ('1' !== $days) { echo 's'; }` This will output '# day' and apply an 's' if it does not equal '1'. This also accounts for '0', as you say '0 days' not '0 day'. – kittycat Oct 29 '16 at 20:04