Very specific date formatting in PHP - php

I'm trying to import a date "9:26 AM Thursday Feb 11, 2016" but it only shows "12:00 AM Thursday Feb 11, 2016"
$date = date("h:i A l M j, Y", strtotime("9:26 AM Thursday Feb 11, 2016"));echo $date;

If you use the incredibly flexible DatTime class it is quite easy.
And using the ->format() method once you have a valid date loaded you can output it in any format you like
$d = DateTime::createFromFormat('h:i A l M j, Y', "09:26 AM Thursday Feb 11, 2016");
echo $d->format('h:i A l M j, Y');
echo $d->format('d/m/Y H:i');
echo $d->format('d/m/Y H:i l');
Gives this
09:26 AM Thursday Feb 11, 2016
11/02/2016 09:26
11/02/2016 09:26 Thursday

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

Convert String Date to Object

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

PHP date wrong textual day, but date correct

This is weird, i'm trying to echo out a date like Monday, January 1, 2013, but its echoing out the wrong textual day. I don't have a clue why?
I have:
<?php echo date('l, F n, Y', strtotime($do['dueDate'])); ?>
And $do['dueDate'] is the date from the database of "2013-03-22". Its formatted as DATE in mysql.
When the above echos out it says: Friday, March 3, 2013
But march 3, 2013 is a sunday...
Try like this
echo date('l ,F j ,Y', strtotime($do['dueDate']));
Try this :
n --> Numeric representation of a month, without leading zeros --> 1 through 12
j --> Day of the month without leading zeros --> 1 to 31
<?php
$do['dueDate'] = "2013-03-22";
echo date('l, F j, Y', strtotime($do['dueDate']));
?>
Output :
Friday, March 22, 2013
use this
<?php
echo date('l, F j, Y', strtotime("2013-03-22")); // output Friday, March 22, 2013
working example http://codepad.viper-7.com/rF6w1U
http://php.net/manual/en/function.date.php
n is the numerical representation of the month.
try this:
$date = "2013-03-22";
echo date('l, F j, Y', strtotime($date));
outputs:
Friday, March 22, 2013

Convert Date Format

I have created a script through which i retrieve values from a rss feed.
One value is in following date format:
Thu, 14 Apr 2011 18:23:19 GMT
What i wanted this value to be in following format:
April 14, 2011 11:53 PM
How can i achieve this?
Please help me on this
Thanks
Pankaj
The DateTime class is handy for this. You can use its format() method.
$datetime = new DateTime('Thu, 14 Apr 2011 18:23:19 GMT');
echo $datetime->format('F j,Y g:i A');
$olddate = "Thu, 14 Apr 2011 18:23:19 GMT";
$newdate = date("F d, Y H:i A",strtotime($oldDate));
Try it:
echo (date('F j,Y g:i:A',strtotime($Yourinputdate)));
<?php
// Assuming today is March 10th, 2001, 5:16:18 pm, and that we are in the
// Mountain Standard Time (MST) Time Zone
$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
?>
above code taken from : http://www.php.net/manual/en/function.date.php
i didnot see you have an input so here it is:
$date = new DateTime('Thu, 14 Apr 2011 18:23:19 GMT');
echo $date->format("F j, Y g:i a");
Try this and see if it helps
echo(date('F j,Y g:i:A',strtotime(Date_From_RSS)));

Convert date in php

How to convert "Thu Oct 28 16:33:29 +0000 2010 " to like "Oct 28,2010 at 10:33PM"
Use the strtotime function combined with the date function
echo date("jS F, Y", strtotime("11/25/10"));
// outputs 25th November, 2010
$date = DateTime::createFromFormat('Thu Oct 28 16:33:29 +0000 2010', 'D M j h:m:s O Y');
echo $date->format('M d,Y, h:mA');
Relevant docs here

Categories