PHP DateTime::createFromFormat return wrong date - php

I've got a date like : $date = DateTime::createFromFormat('D d/m', 'Mon 05/02'); but instead of 05 february the datetime returned is DateTime Object ( [date] => 2021-02-08 10:02:10.000000 [timezone_type] => 3 [timezone] => Europe/Brussels )
Answer
Corrected with the Y input and got the right result, php was using 2021 when i was constructing 2022 year

If the (wrong) day of the week is to be ignored, then an * only needs to be set in the format instead of the "D".
$date = DateTime::createFromFormat('* d/m', 'Mon 05/02');
"Mon" is ignored and the expression "05/02" is used to determine the date.
DateTime::__set_state(array(
'date' => "2021-02-05 18:28:31.000000",
'timezone_type' => 3,
'timezone' => "Europe/Berlin",
))

Because in 2021, February 5 is Friday, and February 8 is Monday.

Related

time relative formats : get the date of the first Sunday of last year

I want to retrieve the date of the first Sunday of last year,
but i have a problem
$date = new DateTime('first sunday of august last year');
gives me as a result :
DateTime Object ( [date] => 2020-08-01 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin )
// saturday
but I want :
DateTime Object ( [date] => 2020-08-02 00:00:00.000000 [timezone_type] => 3 [timezone] => Europe/Berlin )
// sunday
is this a bug or am I at fault ?
It seem that last year fail to get the right date.
Seem confused between first sunday of august 2021 (08-01) and 2020 (08-02).
Using "hard coded" year, give the right date.
new DateTime('first sunday of august 2020', new DateTimeZone('Europe/Berlin'));
// "2020-08-02 00:00:00.000000"
A quick workaround could be :
$year = date('Y', strtotime('-1 year'));
$date = new DateTime('first sunday of august ' . $year, new DateTimeZone('Europe/Berlin'));
// "2020-08-02 00:00:00.000000"
The reason is
"ordinal dayname 'of' " does not advance to another day. (Example: "first wednesday of july 23rd, 2008" means "2008-07-02" because the specific phrase with 'of' resets the day-of-month to '1' and the '23rd' is ignored here).
You get the right answer if you specify the year:
$date = new DateTime('first sunday of august 2020');

PHP and DST conversion issue

I'm having trouble understanding how my code adapts to DST, as it's incorrect with the recent update. I'm storing a date time in the database based on UTC and then converting it back to the local timezone for display. If PHP is taking DST into account, something else is wrong because all of my stored dates are 1 hour off.
$stored_date = '2016-11-16 12:04:01'; // in UTC
$dateTime = new DateTime($stored_date, new DateTimeZone('UTC'));
$dateTimeZone = new DateTimeZone('America/New_York');
$dateTime->setTimezone($dateTimeZone);
print_r($dateTime);
Last week, before DST ended, this would have printed out 2016-11-16 08:04:01. This week, now that DST has ended, it prints out 2016-11-16 07:04:01. Why the hour difference if PHP is properly handing the DST shift?
It shouldn't matter the server settings (I don't think) because I'm explicitly doing the conversion within PHP, right?
I'm ready to start doing a check with PHP to see if DST is in effect and offsetting the conversion by 1 hour because I can't figure out why that hour isn't being automatically compensated for within the DateTime class.
New York city switches between these time zones:
Winter: EST (Eastern Standard Time) = UTC -5
Summer: EDT (Eastern Daylight Time) = UTC -4
According to timeanddate.com the switch will happen on 6th of November. Thus the result is correct: 12 - 5 = 7
In other words, PHP is perfectly aware of DST, as we can see in the following code:
$dateTime = new DateTime('2016-11-05 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
$dateTime = new DateTime('2016-11-06 12:04:01', new DateTimeZone('UTC'));
$dateTime->setTimezone(new DateTimeZone('America/New_York'));
echo $dateTime->format('r') . PHP_EOL;
Sat, 05 Nov 2016 08:04:01 -0400
Sun, 06 Nov 2016 07:04:01 -0500
You can inspect the exact information available in your system's time database:
$timeZone = new DateTimeZone('America/New_York');
print_r($timeZone->getTransitions(mktime(0, 0, 0, 1, 1, 2016), mktime(0, 0, 0, 12, 31, 2016)));
Array
(
[0] => Array
(
[ts] => 1451602800
[time] => 2015-12-31T23:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
[1] => Array
(
[ts] => 1457852400
[time] => 2016-03-13T07:00:00+0000
[offset] => -14400
[isdst] => 1
[abbr] => EDT
)
[2] => Array
(
[ts] => 1478412000
[time] => 2016-11-06T06:00:00+0000
[offset] => -18000
[isdst] =>
[abbr] => EST
)
)

Strange bug of date_modify function in DateTime class

I use the native PHP DateTime class for adding days to dates. But when dealing with negative dates, I encountered a strange bug. Depending on the millennium added or a day or two. Example:
$date_one = date_create("-1000-12-27");
date_modify($date_one, '+1 day');
//Return DateTime Object ( [date] => -1000-12-29 00:00:00 )
$date_two = date_create("-2000-12-27");
date_modify($date_two, '+1 day');
//Return DateTime Object ( [date] => -2000-12-28 00:00:00 )
$date_three = date_create("-3000-12-27");
date_modify($date_three, '+1 day');
//Return DateTime Object ( [date] => -3000-12-29 00:00:00 )
That is, depending on the parity of the millennium issue, or December 28 or December 29. Why is this happening? What is the problem?

PHP DateTime, parsing string date fails

im passing a startDate end an endDate as a get request parameter to a method,
here they get parsed like :
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$logger->info('startdate is :'.$startDate.', endDate is : '.$endDate.'');
$start=new \DateTime($startDate);
$end=new \DateTime($endDate);
when i log those two parameters, they may be
startdate is: Wed Jan 12 2011 00:00:00 GMT 0100 (CET)
startDate is: Sat Jan 12 2013 00:00:00 GMT 0100 (CET)
so far so good, but if i log the DateTimeĀ“s instanciated from the string above it returns
DateTime Object ( [date] => 0100-01-12 00:00:00 [timezone_type] => 2 [timezone] => GMT )
DateTime Object ( [date] => 0100-01-15 00:00:00 [timezone_type] => 2 [timezone] => GMT )
you can see, the DateTime does not represent the same Date
can i make a valid DateTime from those Strings ?
Update :
i tryed to use createFromFormat
like
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$start=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate);
$end=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate);
but that causes exception :
FatalErrorException: Parse: syntax error, unexpected 'createFromFormat' (T_STRING), expecting variable (T_VARIABLE) or '$' in
i also tryed :
$start=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate));
$end=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate));
But that creates Dates a new Date from right now ( 2014-01-21 12:28:57 )
I just dont get it right.
for any help, thanks in advance!
Your input datetime string Wed Jan 12 2011 00:00:00 GMT 0100 (CET) is not valid/standard for use in DateTime() or strtotime(). See date_parse() function to see how your datetime string is being parsed:
print_r( date_parse('Wed Jan 12 2011 00:00:00 GMT 0100 (CET)') );
demo
Use DateTime::createFromFormat() static method to return DateTime object according to the specific format.
demo
The date format you are probably using is RFC2822. As shown on the PHP date() page as this:
Thu, 21 Dec 2000 16:01:07 +0200
You switched the month and day parts and PHP was unable to determine the correct parts.
Best practice would be to either use a Unix-Timestamp (seconds after Epoch) or a better format like ISO 8601 (2004-02-12T15:19:21+00:00).

break apart formatted date into individual month/day/year hour/minute/second in php

I have a date in the following format
November 18, 2009, 3:00PM
How can i break that up so that i can store each value as its own variable?
such as...
$month //November
$day //18
$year //2009
$hour //03
$minute //00
$ampm //PM
Use the 'date_parse' (http://nl2.php.net/manual/en/function.date-parse.php) function. It returns an array with the parsed items:
Array
(
[year] => 2006
[month] => 12
[day] => 12
[hour] => 10
[minute] => 0
[second] => 0
[fraction] => 0.5
[warning_count] => 0
[warnings] => Array()
[error_count] => 0
[errors] => Array()
[is_localtime] =>
)
Convert your date into a timestamp, then with the timestamp you can easily get your parts. An other way is using a regular expression.
$str = "November 18, 2009, 3:00PM";
list($month,$day,$year,$time) = preg_split('/[ ,]/',$str,false,PREG_SPLIT_NO_EMPTY);
preg_match('/([0-9]+):([0-9]+)([AP]M)/',$time,$timeparts);
list($time,$hour,$minute,$ampm) = $timeparts;
echo "\$month $month\n";
echo "\$day $day\n";
echo "\$year $year\n";
echo "\$hour $hour\n";
echo "\$minute $minute\n";
echo "\$ampm $ampm\n";
Output
$month November
$day 18
$year 2009
$hour 3
$minute 00
$ampm PM
More complex solution.
If your dates may be in the different standards you can use date() function (http://php.net/manual/en/function.date.php) + strtotime() function (http://php.net/manual/en/function.strtotime.php), which parse string and returns the unix timestamp.
For example, if you want to get a year from your date string you could write next code:
$date = 'November 18, 2009, 3:00PM';
$year = date('Y', strtotime($date));
Or, if you want to know how much days in the month in date you get, you could write such code:
$date = 'November 18, 2009, 3:00PM';
$num_of_days = date('t', strtotime($date));
't' returns the number of days in the given month.

Categories