PHP convert date to yyyy/MM/dd HH:mm - php

I am receiving a date from Apple as Mar 5, 2020 at 9:18 AM. I believe this is M j, Y at G:i A but their string has an added at.
I tried to do this..
$origDate = "Mar 5, 2020 at 9:18 AM"
$date = DateTime::createFromFormat('M j, Y at G:i A', $origDate);
echo $date->format('yyyy/MM/dd HH:mm');
But I get the following error.
Uncaught Error: Call to a member function format() on boolean
How can I fix this?

All Date format identifiers are single characters (Y), not multiple (YYYY). Also, actual strings (at) will need to be escaped. You would need to create the DateTime object like this:
$origDate = "Mar 5, 2020 at 9:18 AM";
$date = DateTime::createFromFormat('M d, Y \a\t g:i A', $origDate);
echo $date->format('Y/m/d H:i');
As I mentioned in my comment, you can see all of the format identifiers at https://www.php.net/manual/en/datetime.createfromformat.php

Related

Parsing Datetime in PHP

I have the following date forma string: January 5, 2016 07:22am ET
I would like to be able to parse such date format using DateTime::createFromFormat. I have tried with the following and no success:
$new_date = DateTime::createFromFormat($date, 'D d, Y G:i e');
where $date is the string January 5, 2016 07:22am ET
Any idea?
Sets the time zone for the DateTime object
$format = 'Y-m-d';
$date = DateTime::createFromFormat($format, '2016-05-27');
$new_date=$date->format('D d, Y G:i e') ;
echo $new_date;
And Your Output Will Be
Fri 27, 2016 12:51 UTC
Example:
$date = new DateTime(
'January 5, 2016 07:22am', //your time string
new DateTimeZone('UTC') //set you timezone
);
echo $date->format('F d, Y h:ia e');
will output
January 05, 2016 07:22am E
The ET is incorrect Timezone word for PHP.
Without that You can create DateTime like this:
$date = new DateTime('January 5, 2016 07:22am');

How to print "at" inside php date function?

i tried using date("F j, Y \a\t h:i a", strtotime($date));
but this produces May 20, 2013 a 04:37 pm
i also tried date("F j, Y \at\ h:i a", strtotime($date)); and
date("F j, Y \a\t\ h:i a", strtotime($date));
and it became May 20, 2013 a31 04:37 pm
what i want to produce is this May 20, 2013 at 04:37 pm
Thanks!
With double quotes \t will be interpreted as a tab character. Using single quotes (or double slashes) takes care of the issue:
echo date('F j, Y \a\t h:i a', strtotime('2013-05-20 23:59:59'));
// output: May 20, 2013 at 11:59 pm

How to append a string in date function first parameter?

I have following code
$date = '2013-05-11 07:10:14';
echo date('F j, Y at h:i a', strtotime($date); //Not work; May 11, 2013 at 7:10 am
echo date('F j, Y h:i a', strtotime($date); // This will work when avoiding the `at` from date function.
I am trying to append string in date function to display the above date as May 11, 2013 at 7:10 am. How to make date at this format using prebuilt date function?
Try escaping the at string,
echo date('F j, Y \a\t h:i a', strtotime($date));
DEMO.
You need to escape any letters that are also formatting queues. You have to double escape the "t" because \t is the tab character.
echo date('F j, Y \a\\t h:i a');
See it in action

Date Format is not working

i have a string that is like this 2012/10/12 10:03:46 (Year/Month/Day) format now i want to change its format to something like this October 12, 2012 10:03 p.m i have tried php's DateTime class but its not working:
<?php
$date = new DateTime();
$date->createFromFormat('Y/m/dd H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('d-m, y h:i A');
?>
can anyone tell me whats wrong and how can i correct it??
You should have seen at least one notice something like "don't call static function in non-static context".
DateTime::createFomFormat()
$date = DateTime::createFromFormat('Y/m/d H:i:s', substr($suggestion->suggestion->created_at, 0,19));
echo $date->format('F j, Y, g:i a');
It's a static method and (as the name suggests) it creates a new instance of DateTime.
$today = date("F j, Y, g:i a");
<?php
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('d-M, y h:i:s A');
//output - 12-Oct, 12 10:03:46 AM
?>
Try this...
$today = date("F j, Y h:i a"); // gives October 15, 2012 5:20 PM
Try this...
$today = date("F j, Y g:i A"); // gives October 15, 2012 5:20 PM (g removes leading zeroes)
Your case
$date = new DateTime('2012/10/12 10:03:46');
echo $date->format('F j, Y h:i A'); // gives October 15, 2012 10:03 AM
Reference : click

PHP date showing weird year/date from timestamp

I have the following timestamp:
1341111034380
Which equates to:
Sun, 01 Jul 2012 02:50:34 GMT
However, when I try to format this using:
date("F j, Y, g:i a", 1341111034380)
I get:
February 12, 44468, 5:53 am
Any ideas why it would do this?
You have the wrong units.
1341111034380 is in milliseconds, whereas a proper timestamp is in seconds, which is what PHP expects.
If you want to use it in PHP, you should convert it to seconds.
echo date("F j, Y, g:i a", floor(1341111034380/1000));
use:
echo date("F j, Y, g:i a", substr(1341111034380, 0, 10));

Categories