Convert String Date to Object - php

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");

Related

How to Convert Date and Time String into Different format?

I am saving date and time in a single column i.e 2015-04-15 13:22:58 and I want to show this time and date in this format: 15th April 2015, 1:22 pm.
I have used this code:
$dt = '2015-04-15 13:22:58';
echo date("jS F Y", strtotime($dt));
and it shows only date (15th April 2015). How can I fetch time along with date?? Any help would be highly appreciable.
Try
$dt = '2015-04-15 13:22:58';
echo date("jS F Y h:i a", strtotime($dt));
//Result is 15th April 2015 01:22 pm
echo date("jS F Y g:i a", strtotime($dt));
// Result is 15th April 2015 1:22 pm
change "jS F Y" to "js F Y g:i a". That will output the time in your desired format as well as the date.
$dt = '2015-04-15 13:22:58';
echo date("jS F Y g:i a", strtotime($dt));
Output:
15th April 2015 1:22 pm
if you want to format date time in php, you can use date_format like this.
echo date_format(strtotime($dt), "jS F Y g:i A");

how to convert DateTime format [2016-10-05 11:58:04] using DateTime::createFromFormat

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

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');

convert date and time in php

i just need date format like this "26 March 2015 - 11:59 pm"
how to get 'pm'in my code
$month_year=date('F Y');
$timestamp = strtotime("$month_year");
$result = date('t F Y - 11:59 a', $timestamp);
Try below code
$date = date("d F Y - h:m a");
Just use DateTime like this:
$date = DateTime::createFromFormat("h:i a", "11:59 pm");
echo $date->format("d F Y - h:i a");
Output:
09 March 2015 - 11:59 pm
Check this
$month_year=date('F Y');
$timestamp = strtotime("$month_year");
echo $result = date('t F Y - H:i a', $timestamp);

How to convert yyyy-MM-dd HH:mm:ss to "15th Apr 2010" using PHP

I have the following date format: 2010-04-15 23:59:59
How would I go about converting this into: 15th Apr 2010
Thanks
echo date("jS M Y",strtotime("2010-04-15 23:59:59"));
In addition to using date and strtotime, you can do
$dateTime = new DateTime('2010-04-15 23:59:59');
echo $dateTime->format('jS M Y'); // 15th Apr 2010
or
$dateTime = date_create('2010-04-15 23:59:59');
echo date_format($dateTime, 'jS M Y'); // 15th Apr 2010
$date = date("dS M Y",strtotime("2010-04-15 23:59:59"));
print $date;

Categories