note 88867 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Wed, 11 Feb 2009 16:50:49 +0000 |
| Subject: | note 88867 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-150497@lists.php.net to get a copy of this message | ||
Here's a revised version of [code I originally wrote on 21-JAN-09]. You can work out the
number of days left in the month or year like this. [This version] also works with arbitrary dates
given to it rather than only working with the current date:
<?php
function days_left_in_month($isoDate) {
$date = explode('-', $isoDate);
$date[1]++;
$date[1] = str_pad($date[1], 2, '0', STR_PAD_LEFT);
if ($date[1] == '13') {
$date[0]++;
$date[1] = '01';
}
$date[2] = '01';
$isoDateNextMonth = implode('-', $date);
return ceil((strtotime($isoDateNextMonth) - strtotime($isoDate)) / 86400);
}
echo days_left_in_month('2009-01-31'); // Much better
?>
[EDIT BY danbrown AT php DOT net: Contains bugfixes by (akniep AT rayo DOT info) on 03-FEB-09 and
the original author.]
--was--
akniep is completely right. My previous example is indeed buggy. Thanks for pointing this out,
akniep!
Here's a revised version of the code. It also works with arbitrary dates given to it rather
than only working with the current date:
function days_left_in_month($isoDate) {
$date = explode('-', $isoDate);
$date[1]++;
$date[1] = str_pad($date[1], 2, '0', STR_PAD_LEFT);
if ($date[1] == '13') {
$date[0]++;
$date[1] = '01';
}
$date[2] = '01';
$isoDateNextMonth = implode('-', $date);
return ceil((strtotime($isoDateNextMonth) - strtotime($isoDate)) / 86400);
}
echo days_left_in_month('2009-01-31'); // Much better
http://php.net/manual/en/function.strtotime.php