Converting ISO 8601 format to d M Y in PHP - php

PHP code:
echo date("c"); //out put like this 2012-06-19T20:37:44+05:30
echo date("d M, Y"); //out put "Jun 19,2012"
$time=date("c");
echo date("d M, Y",$time);// This is not working. What could the reason be?
My requirement is to convert ISO8601 format to 'd M ,Y'. I need it in this format to use with the timeago jQuery plugin.

You need to use the strtotime() function.
echo date("d M, Y",strtotime(date("c")));

Alternatively, you can use PHP's DateTime object to perform the conversion:-
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, date('c'));
echo $dateTime->format('d M Y');

The issue is that the second argument to date is expected to be a timestamp, you are passing a string. Use strtotime to convert your date to into a timestamp:
$time = date("c");
echo date("d M, Y", strtotime($time));

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.

Regex to convert already formatted date to different format

I have $date = $run['at'];, which gives me 2013-06-03T16:52:24Z. How can I manipolate it to get for example "d M Y, H:i" ?
You should use the DateTime class.
$date = new DateTime($run['at']);
echo $date->format('d M Y, H:i');
You can use:
$time = strtotime($run['at']);
echo date("Y-m-d", $time);
But, where does $run['at'] come from?

How to format date yy-mm-dd in PHP

I have dates currently in this format: yy-mm-dd (e.g. 2011-11-18)
I want them in this format: Friday 18 November 2011
I've tried reading through the PHP documentation manual, but I can't see how to manage dates in the format that I have. If the date needs to be in a different order I can arrange that, but I'm a bit stuck at the meoment.
Any help would be much appreciated.
Use PHP5s new date classes. Much cleaner:
$date = DateTime::createFromFormat('Y-m-d', '2011-11-18');
echo $date->format('l d F Y');
date('l j F Y', strtotime($date));
Just use starttime to change the the dates in many formats using this link.
echo date('l d F Y');
gives you the date format you want.
This was all in the manual you yourself linked.
just use strtotime to get back a timestamp and then use date() to format that:
$date = '2011-11-18'; // your date
$timestamp = strtotime($date); // convert to a timestamp
$new_date = date('l j F Y',$timestamp) // format timestamp
echo $new_date;

PHP Convert a Date into this format [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP convert one date into another date format
This is PHP
I have this result:
2011-09-20 13:00:00
I want to convert it into this format:
September 20 2011 1:00 pm
Do you know how to do that?
Anyhelp would be greatly appreciated.
Thanks!
try this:
$old_date = '2011-09-20 13:00:00';
$old_date_timestamp = strtotime($old_date);
$new_date = date('F j Y g:i a', $old_date_timestamp);
If that result comes from an object of type DateTime you can use the format function:
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
and here all the formats you can have.. change it according to your needs.
You can get the UNIX-timestamp of a time string with strtotime() and you can get a differently designed time string with strftime()
For more information you can read the documentation here : http://php.net/manual/en/function.date.php
But also is simple. Lets see how
$d = "2011-09-20 13:00:00";
$d = strtotime($d);
$d = date("F m Y g:i a", $d);
echo $d;
Take a look at these PHP functions:
http://nl.php.net/manual/en/function.date.php
and
http://nl.php.net/strtotime
To do something like this:
date('F d Y G:i',strtotime("2011-09-20 13:00:00")); // your required format
You can use this format
$date= date("F j Y g:i a");
if you have date in any variable then
$date= date("F j Y g:i a",strtotime($your_variable));
echo $date
echo date("F d Y g:i a",strtotime("2011-09-20 13:00:00 "));
echo date('F d Y g:i a', strtotime('2011-09-20 13:00:00'));
check the date format

PHP date conversion

I have date in format 2011-01-28 06:34:33 i.e. date("Y-m-d H:i:s"). I want to convert it into 28th January 2011.
How can I change it?
Supply the date function with your format, which can be found here. Pass the timestamp of your original date as the second parameter to date. You can obtain the timestamp by using strtotime.
date("dS F Y", strtotime("2011-01-28 06:34:33"));
Use
$dateStr = date("jS F Y", time());
The day value is without leading zero.
Try this echo date('jS F Y h:i:s A');

Categories