|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-05-24 15:47 UTC] fcasadei at fcpartner dot com
Description: ------------ --- From manual page: http://www.php.net/datetime.diff --- $objDateTo = new dateTime('2017-10-01'); $objDateFrom = new dateTime('2017-01-01'); $interval = $objDateTo->diff($objDateFrom); echo $interval->m; will display 8 $objDateTo = new dateTime('2017-10-01 12:00:00'); $objDateFrom = new dateTime('2017-01-01 12:00:00'); $interval = $objDateTo->diff($objDateFrom); echo $interval->m; will display 9 Expected result: ---------------- Should be 9 in both cases PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
If you define your timezone: ini_set("date.timezone", "GMT"); Both work nicely https://3v4l.org/SuVe2 ini_set("date.timezone", "GMT"); $objDateTo = new dateTime('2017-10-01') $objDateFrom = new dateTime('2017-01-01 $interval = $objDateTo->diff($objDateFr echo "Date Only: " . $interval->m . "\r $objDateTo = new dateTime('2017-10-01 1 $objDateFrom = new dateTime('2017-01-01 $interval = $objDateTo->diff($objDateFr echo "Date and Time: " . $interval->m;thanks but with ("date.timezone", "Europe/Paris"), back to 8 and 9 !! Is PHP another american centric stuff ? ini_set("date.timezone", "Europe/Paris"); $objDateTo = new dateTime('2017-10-01'); $objDateFrom = new dateTime('2017-01-01'); $interval = $objDateTo->diff($objDateFrom); echo $interval->m; $objDateTo = new dateTime('2017-10-01 00:00:00'); $objDateFrom = new dateTime('2017-01-01 00:00:00'); $interval = $objDateTo->diff($objDateFrom); echo $interval->m; $objDateTo = new dateTime('2017-10-01 12:00:00'); $objDateFrom = new dateTime('2017-01-01 12:00:00'); $interval = $objDateTo->diff($objDateFrom); echo $interval->m; gives 889 !! 12 hours make a month ???Given that the interval changed when the time was specified, I did suspect that something happened sometime in the morning the start or the afternoon of the end that changed the length; but No, Europe/Paris doesn't have anything special to say about those dates. I looked at a few other zones and intervals. (a) Asia/Baghdad also displays varying results despite not crossing a DST boundary (b) America/Denver is consistent, even though it does cross a DST boundary. (c) The period from 1 January to 1 February gives consistent results for all zones (d) The period from 1 July to 1 August does not; this time though the affected zones are Europe/Paris, Europe/Lisbon, Asia/Baghdad, and Pacific/Auckland. (e) The number of days is always the same. For the January-October period, it is 273; the variation seen in Europe/Paris and Asia/Baghdad is that 273 days makes nine months when counting from midnight, but only eight months 30 days when counting from midday. <?php $zones_to_try = [ "UTC", "Europe/Paris", "Europe/Lisbon", "Asia/Baghdad", "Pacific/Auckland", "America/Phoenix", "America/Denver", ]; foreach($zones_to_try as $zone) { echo "$zone:\n"; date_default_timezone_set($zone); $objDateTo = new dateTime('2017-10-01'); $objDateFrom = new dateTime('2017-01-01'); $interval = $objDateTo->diff($objDateFrom); echo "From {$objDateFrom->format('c')} to {$objDateTo->format('c')}: {$interval->m}m {$interval->d}d ({$interval->days} days)\n"; $objDateTo = new dateTime('2017-10-01 12:00:00'); $objDateFrom = new dateTime('2017-01-01 12:00:00'); $interval = $objDateTo->diff($objDateFrom); echo "From {$objDateFrom->format('c')} to {$objDateTo->format('c')}: {$interval->m}m {$interval->d}d ({$interval->days} days)\n"; echo "\n\n"; } ?>