Date Format is not working - php

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

Related

php change the string into time format

In php I have time like this
$time = '2015-06-29T16:00:00Z';
I want to convert that time like this format Tuesday, December 16, 2015 3:00 PM
For that I tried
echo date( 'jS F Y', strtotime( $time) );
but it is showing time like 1st January 1970
So can someone help me to get the actual time format as I want.
A simple DateTime class usage should suffice, just feed it into the constructor, the just use ->format and provide the desired output format:
$time = '2015-06-29T16:00:00Z';
$date = new DateTime($time);
echo $date->format('jS F Y');
Sample Output
You can use the DateTime class for better handling of dates
$time = '2015-06-29T16:00:00Z';
$dateTime = new DateTime($time);
echo $dateTime->format('l, F d, Y g:i A');
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y H:i A',strtotime($time));
l, F j, Y H:i A can be re-ordered to change the output.
About date function, http://php.net/manual/en/function.date.php
Just pass proper format parameters to it.
$time = '2015-06-29T16:00:00Z';
echo date( 'l, F j, Y g:i A', strtotime( $time) );
Use preg_split:
$parts = preg_plit("/Z/",$time);
$parts = preg_split("/T/",$parts[0]);
$theDate=$parts[0];
$theTime=$parts[1];
$what_you_want=date(strtotime($theDate." ".$theTime);
Note that you can still change the format of the output.

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

PHP Date - How to add a string to separate date and time

I want to display date and time format something like this "May 23 at 12:30pm".
I saw in PHP manual and found:
// Prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
After modification I manage to get
echo date('M j \of h:i a');
it is giving me "May 23 of 12:30pm"
but when i replacing of with at it is giving me "May 23 a23 08:26 pm".
I don't what is going wrong.
you need to escape the a and t as both have special meaning when used as formatting options in date()
echo date('M j \a\t h:i a');
See it in action
Try
<?php
echo date('M j \a\t h:i a');
?>
OR
<?php
echo date('M j'). "at". date(' h:i a');
?>
You need to escape the t too:
echo date('M j \a\t h:i a');
Another option could be:
echo date('M j')." at ".date('h:i a');
You need to try this in php:
$t = now(); //This will give you current time
echo date_format($t,"dS M,y \a\\t h:i a"); //14th May,19 at 05:39am

How do I convert this time format?

2011-07-13 02:38:31
^ MySQL format.
I would like to use PHP to convert it to another format like , July 13, 2011 2:38 am.
<?php
$time_from_db = '2011-07-13 02:38:31';
echo date('F j, Y g:i a', strtotime($time_from_db));
?>
echo date("F j, Y g:i a", strtotime("2011-07-13 02:38:31"));
demo
$yourdatevar = date("F j, Y, g:i a", strtotime($yourdatevar)); // March 10, 2001, 5:16 pm
Taken right from: http://php.net/manual/en/function.date.php

formatting the date in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Format mysql datetime with php
I take and store a date into a mysql database. It displays like this:
2011-03-17 17:49:49
But I want it to show like this instead:
Thur 17 March 2011 5:49 PM
use date() in PHP: http://us.php.net/manual/en/function.date.php
or
format your output in mysql: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Convert it into a timestamp with strtotime(), and then format it with date() in PHP.
You should do formatting with date function.
date('D d F Y g:i a', strtotime($date));
Refer to manual for more.
Use the below function
date("D j F, Y, g:i a");
$today = date(”F j, Y, g:i a”); -> February 5, 2010, 6:20 pm
refer these links it may help u link1 link2 link3
$time = '2011-03-17 17:49:49';
$date = new DateTime($time);
echo $date->format('D j F Y g:i A'); // Thu 17 March 2011 5:49 PM
Note that in your example, you have Thur and not Thu as per my output. PHP doesn't have any native character to represent this, but you could do...
$time = '2011-03-17 17:49:49';
$date = new DateTime($time);
echo substr($date->format('l'), 0, 4) . $date->format(' j F Y g:i A');
// Thur 17 March 2011 5:49 PM
best to store dates in the db as unix time stamp then when there outputted use something like this to display it how you want
<?php
// echo date ( "F j, Y, g:i a", timestamp );
// Assuming today is: March 10th, 2001, 5:16:18 pm
$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 z '); // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$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 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h'); // 17:03:17 m is month
$today = date("H:i:s"); // 17:16:17
?>
$given_date = '2011-03-17 17:49:49';
echo $ur_date = date('D j F Y g:i:s A',strtotime($given_date));
http://www.php.net/manual/en/datetime.createfromformat.php

Categories