note 118462 added to datetime.add

From: Date: Thu, 10 Dec 2015 16:22:13 +0000
Subject: note 118462 added to datetime.add
Groups: php.notes 
Request: Send a blank email to php-notes+get-204321@lists.php.net to get a copy of this message
Be careful when using this function, I may have happened upon a bug in PHP7. My code is as follows //get date from post or else fill with today's date if (isset($_POST["from"])) { $from = date_create($_POST["from"]); }else{ $from = date_create(date("Y-m-d")); } //get date from post if there isn't one just take the same date as what is in the $from variable and add one day to it if (isset($_POST["to"])) { $to = date_create($_POST["to"]); }else{ $to = $from; date_modify($to, '+1 day'); } echo(date_format($from, 'Y-m-d') . " " . date_format($to, 'Y-m-d')); The resultant output is $from = 2015-12-11 $to = 2015-12-11 In actuality the result should be $from = 2015-12-10 $to = 2015-12-11 For some reason the code above modifies the $from variable in the line date_modify($to, '+1 day'); even though it shouldn't as the $from variable isn't being modified. to fix this i needed to change the code to //get date from post or else fill with today's date if (isset($_POST["from"])) { $from = date_create($_POST["from"]); }else{ $from = date_create(date("Y-m-d")); } //get date from post if there isn't one just take the same date as what is in the $from variable and add one day to it if (isset($_POST["to"])) { $to = date_create($_POST["to"]); }else{ $to = date_create(date("Y-m-d")); date_modify($to, '+1 day'); } echo(date_format($from, 'Y-m-d') . " " . date_format($to, 'Y-m-d')); This isn't strictly the code I wanted. Possible bug? ---- Server IP: 72.52.91.14 Probable Submitter: 164.39.7.250 ---- Manual Page -- http://php.net/manual/en/datetime.add.php Edit -- https://master.php.net/note/edit/118462 Del: integrated -- https://master.php.net/note/delete/118462/integrated Del: useless -- https://master.php.net/note/delete/118462/useless Del: bad code -- https://master.php.net/note/delete/118462/bad+code Del: spam -- https://master.php.net/note/delete/118462/spam Del: non-english -- https://master.php.net/note/delete/118462/non-english Del: in docs -- https://master.php.net/note/delete/118462/in+docs Del: other reasons-- https://master.php.net/note/delete/118462 Reject -- https://master.php.net/note/reject/118462 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204321) next »