PHP DateTime::createFromFormat AM PM Issue - php

$format = 'd M Y A h:i';
$date = DateTime::createFromFormat($format, '11 Mar 2013 PM 3:34');
Returns false.
getLastErrors returs:
[errors] => Array
(
[12] => Unexpected data found.
)
After removing A in format and PM in date it started to work.
$format = 'd M Y h:i';
$date = DateTime::createFromFormat($format, '11 Mar 2013 3:34');
Any solution how to make it working with AM and PM ?

I would append the AM/PM after the time. See http://php.net/manual/en/datetime.formats.time.php for accepted time formats.

Related

PHP DateTime::createFromFormat return wrong date

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.

Sugarcrm date not showing properly

SCENARIO
So I've custom object where I've three fields called start_date, end_date and booking_expire_time(for an event).
My dates are coming from Magento in form of String ofc, I already have logged so they are coming like
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $start_date - 08/30/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $event_endt_time - 08/31/2018 11:59 AM
Thu Aug 30 09:46:16 2018 [8779][ffba8854-e7c0-11e6-85b6-06ec5399a877][FATAL] $booking_expire_time - 08/30/2018 12:59 PM
Which is correct and same which I've chosen in Magento
And I'm storing it like following in that bean.
$st = date_create_from_format("m/d/Y H:i A",$start_date);
$eventBean->start_date = date_format($st, 'Y-m-d H:i:s');
$en = date_create_from_format("m/d/Y H:i A",$event_end_time);
$eventBean->event_end_time = date_format($en, 'Y-m-d H:i:s');
$ex = date_create_from_format("m/d/Y H:i A",$booking_expire_time);
$eventBean->booking_expire_time = date_format($ex, 'Y-m-d H:i:s');
ISSUE
There is no problem for saving except when I go to the UI and check date and time, the date and time are different.
I tried changing user timezone and storing it again but it is still same, I tried setting the timezone to UTC in User settings but still, the same thing is happening.
I've logged the date time which getting retrieved in code and it prints following for start date and end date
DateTime Object
(
[date] => 2018-08-30 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
DateTime Object
(
[date] => 2018-08-31 11:59:00.000000
[timezone_type] => 3
[timezone] => UTC
)
EDIT
Timezone setting
PHPInfo from Diagnostic Tool

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).

any function available to find the current date and time of the server in runtime using PHP

any function available to find the current date and time of the server in runtime using PHP as I need to pass this current date and time to the tag "Publish date / Pub date" in RSS feeds.
Please forgive me if the question is so simple.
Regards
Gourav.
date() or getdate(), or any of the many other similar functions that PHP has for dealing with dates.
date() will give you a date based on a format string. There are several builtin format strings, including one for RSS DATE_RSS.
getdate() will give you an associative array containing pretty much all of the info you need:
Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)
from there, your can format it to what RSS needs.
$today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm
$today = date("m.d.y"); // 03.10.01
$today = date("j, n, Y"); // 10, 3, 2001
$today = date("Ymd"); // 20010310
$today = date('h-i-s, j-m-y, it is w Day'); // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.'); // it is the 10th day.
$today = date("D M j G:i:s T Y"); // Sat Mar 10 17:16:18 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:18 m is month
$today = date("H:i:s"); // 17:16:18

Categories