I have a PHP datetime object that I have been trying to convert into a string with DateTime::format() . But this keeps giving me a wrong date . For some reason I keep getting a date that is one day prior the day that I am running my code no matter what value I give the date. My DateTime object -
object(DateTime) {
date => '2017-03-25 00:00:00'
timezone_type => (int) 3
timezone => 'UTC'
}
I use this to format the DateTime into a string -
$myDateObject->format('Y/m/d')
Gives me -
2017/03/23
There is absolutely no code between the line that prints the DateTime object and the line that formats the object ie there are no modifications made on the object .
If it helps the output of my server's date -u
returns Fri Mar 24 06:08:39 UTC 2017
. Also if I create the date string with hours and minutes using $myDateObject->format('Y/m/d h:i')
I get 2017/03/23 12:00
EDIT
The output of my date_default_timezone_get()
is UTC
date('Y-m-d')
gives 2017-03-24
ie today's date
Check out default time zone set in PHP
you may wanna try setting default time zone example: date_default_timezone_set('America/Toronto');