I am using Datetime class on PHP.
you can change datetimeclass to string like this.
$date->format('Y-m-d H:i:s')
// it shows 2013-08-05 10:00:00
but somehow ,Google API requires format like this .
2013-08-05T10:00:00
What this T means ?
and How can I make this style string from DateTime class neatly?
The time is in ISO 8601 format. To print it out, you can use 'c' format character:
$date->format('c')
You could use jh314's solution above, and it will give you the time in following format:
2013-08-08T10:18:15+05:30
However, to format it exactly like you want, you could use the following:
$part1 = $date->format('Y-m-d'); // 2013-08-08
$part2 = $date->format('H:i:s'); // 10:19:37
$newdate = "{$part1}T{$part2}"; // 2013-08-08T10:19:37
Or better yet:
$date = $date->format('Y-m-d\TH:i:s'); // 2013-08-08T10:19:37
Ta-dah!
This is ISO 8601 datetime format check this
$date->format('c') //Output 2004-02-12T15:19:21+00:00
This is almost, but not quite ISO8601 format, so you need to format the output like this:-
$date = new \DateTime();
echo $date->format('Y-m-d\TH:i:s');
The \ escapes the 'T'. See the manual about formatting dates.
See it working
Related
I have much data with several timestamps and I just recognized that some are in "dd.mm.YYYY" which works very well with date("Y-m-d", strtotime($input)); but some are in "dd.mm.YY" and this does not work anymore - it always returns the current date.
My problem is that my data is too huge to fix this problem manually by editting. Is there any way to get the YYYY-mm-dd out of mm.dd.YY ?
Here you go...
$date = "20.02.71"; // sample date... (common German format)
$date = DateTime::createFromFormat('d.m.y', $date);
echo $date->format('Y-m-d');
will result in:
1971-02-20
Create a DateTime object, then format it to anything you want...
Well you can replace the . by -, you could do something like the following:
$date = str_replace(".", "-", "mm.dd.YY")
This would return
mm-dd-YY
You could use date_parse_from_format which would convert any formate into the formate you specify.
date_parse_from_format("y-m-d", $date);
It returns an array with very useful information like month, year etc.
While converting date format from mm-dd-yy hh:ii:ss to yy-mm-dd hh:ii:ss format using below code.
<?php
echo $start_date = date("Y-m-d H:i:s",strtotime("10-14-2015 00:00:00"));
?>
But the result is
1970-01-01 05:30:00
If it is not a proper way to use date ,provide me alternate way
First check this answer whats the difference between dates over here and simply use str_replace like as
echo $start_date = date("Y-m-d H:i:s",strtotime(str_replace("-","/","10-14-2015 00:00:00")));
Or you can also use DateTime::createFromFormat over like as
$date = DateTime::createFromFormat("m-d-Y H:i:s","10-14-2015 00:00:00");
echo $date->format('Y-m-d');
Demo
Read the manual: http://php.net/manual/en/function.strtotime.php
and also specify your PHP version.
It has a nice example on checking if strtotime conversion was successfull:
if (($timestamp = strtotime($str)) === false) {
And this conversion is very critical, it supports only limited number of formats that should be specified very precisely in order days/months/years, and separators / or - or :
So you have to pick the format that you will support from following list:
http://php.net/manual/en/class.datetime.php
Be sure to create culturally aware code (e.g. US/UK/... format)
Please write your code as below:
echo $start_date = date("Y-m-d H:i:s",strtotime("10/14/2015 00:00:00"));
I am looking to convert an EPOCH timestamp (like 1372190184) to a format 2014-06-25T14:38:52.359Z.
I have tried the following code, but the format I get is different from what I need.
$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-dTH:i:sZ');
var_dump($startDateText);
exit();
But I get the output as string(30) "2013-06-25GMT+020021:56:247200" which is different from what I expect.
You forgot the backslashes in your format, and the dollar sign before startDateText in the dump:
$start = new DateTime(date('r', '1372190184'));
$startDateText = $start->format('Y-m-d\TH:i:s\Z');
var_dump($startDateText);
Also, if you're looking for microseconds, add the u format character.
You should be setting the date_default_timezone_set to UTC for your desired output. Format as you wish. And make sure to escape special characters in the format.
date_default_timezone_set('UTC');
$epoch = 1340000000;
echo gmdate('r', $epoch);
You can convert to UTC format date from a date string, for example:
$date = '2022-05-02 11:50:00';
$date = date('Y-m-d\TH:i:s\Z', strtotime($date));
echo $date;
My input date is 2014-03-10 05:40:00. How can I convert it to RFC format like 2014-3-10T05:40:00.000-00:00?
here another option added in php5 like this
$datetime= date("c", strtotime("2014-03-10 05:40:00"));
echo $datetime; //Output : 2014-03-10T05:40:00+00:00
RFC3339 is one of the predefined format constants of the DateTime class.
$inputDate = "2014-03-10 05:40:00";
$datetime = \DateTime::createFromFormat("Y-m-d H:i:s", $inputDate);
echo $datetime->format(\DateTime::RFC3339);
I'd like to add that the predefined constants for this can also be used with date(). Both:
date(DATE_RFC3339);
and
date(DATE_ATOM);
return an RFC3339 formatted date time string and are the equivalent of:
date('Y-m-d\TH:i:sP');
I have a date in this format:
24-12-2010 // DAY - MONTH - YEAR
I need to get it in this format:
1995-12-31T23:59:59.999Z // The Z is for the TimeZone I think.
Check this link out:
http://lucene.apache.org/solr/api/org/apache/solr/schema/DateField.html
The above link is the way I need the date.
I am using PHP now, so this needs to be with PHP.
How can I convert these dates the easiest way?
Thanks
That is an ISO8601 format date; the following is what you want.
gmdate('Y-m-d\TH:i:s\Z', strtotime($date_value));
You can do something like that:
$dateTime = new DateTime($myDate);
$formatted = $dateTime->format("Y-m-d\TH:i:s.z\Z");
The mentioned solution with:
$dateTime->format(DateTime::W3C);
$dateTime->format(DateTime::ISO8601);
does return strings like:
2012-11-28T17:21:11+0100
which cannot be parsed, at least with newer Solr versions.
I wouldn't use gmdate if you need to support timezones. The DateTime implementation is well done, and is also available for functional programming.
http://php.net/manual/en/class.datetime.php
http://php.net/manual/en/ref.datetime.php
You can use the DateTime class
$dateTime = new DateTime();
$dateTime.setDate(24, 12, 2010);
$output = $dateTime.format(DateTime::W3C);
// Output now is your date in W3C format.
use the date ( string $format [, int $timestamp ] ) function of php!
In second paramter use http://php.net/manual/en/function.strtotime.php to get the timestamp from strings
$date = strtotime('24-12-2010');
$new_date = gmDate("Y-m-d\TH:i:s.z\Z",$date);