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');
Related
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
I have a variable holding time in the format Apr 25, 2017 12:00:00 AM I wish to convert it to 2017-04-25 .My result should be without time
You can convert your string formatted date, to timestamp with strtotime() function. Then you can parameter date() function with converted timestamp, and format it with first parameter.
More about formatting:
http://php.net/manual/en/function.date.php
print date('Y-m-d',strtotime('Apr 25, 2017 12:00:00'));
You need to check the date method of PHP
$date = 'May 25, 2017 12:00:00 AM';
$date = date('Y-m-d', strtotime($date));
echo $date;
Try DateTime class:
$datestr = 'Apr 25, 2017 12:00:00 AM';
$date = DateTime::createFromFormat('M d, Y h:i:s A', $datestr);
echo $date->format('Y-m-d');
output
2017-04-25
Use the date() function:
$old_date="Apr 25, 2017 12:00:00 AM";
$new_date=date('Y-m-d', strtotime($old_date));
echo $new_date;
My date is "04 February 2017 - 06:35"
How can I Change into Date Object ?
I tried
$date = date_create_from_format('d/M/Y:H:i:s', "04 February 2017 - 06:35");
But its not Working
Time and the format should be corresponding! Your format is d M Y - H:i.
<?php
$date = DateTime::createFromFormat('d M Y - H:i', "04 February 2017 - 06:35");
echo $date->format('Y-m-d H:i:s');
Try This
You cannot get direct object of string you have to use date_format
$date = date_create_from_format('j M Y - H:i', "04 February 2017 - 06:35");
echo date_format($date,"Y-m-d H:i:s");
i am trying to display a stored DateTime with this format [2016-10-05 11:58:04]. What i want to do is, display the stored date into this readable format [Wed, Oct 10, 2016].
Use date() function. First param is format and 2nd param is timestamp:
$time = "2016-10-05 11:58:04";
echo date("D, M d, Y", strtotime($time)); //output: Wed, Oct 05, 2016
If you want to use datetime object then:
$time = "2016-10-05 11:58:04";
$date = new DateTime($time);
echo $date->format('D, M d, Y'); //output: Wed, Oct 05, 2016
you can use the method format to choose what to display :
<?php
$d = DateTime::createFromFormat("Y-m-d H:i:s", "2016-10-05 11:58:04");
var_dump($d->format("c"));
look the help here : http://php.net/datetime.format
How could I take something that is formatted such as June 15, 2012 06:37PM in the PST timezone and convert it to 06/15/12 06:37PM but in the EST timezone (thus it will be 06/15/12 09:37PM).
If you use PHP >= 5.2.0 you can try this solution:
$date = 'June 15, 2012 06:37PM';
$nDate = DateTime::createFromFormat('F d, Y h:iA' , $date, new DateTimeZone('PST'));
$nDate->setTimezone(new DateTimeZone('EST'));
echo $nDate->format('m/d/y h:iA');
Without using timezones you could just do:
$new_date = strtotime($date) + strtotime("+3 hours");
$new_date = date('m/d/y h:ia', $new_date);