Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert date to T Z format [duplicate]

Tags:

date

php

My date format is something like this

2013-05-07 18:56:57 (yyyy-MM-dd hh:mm:ss) 

I want the output as following.

2013-05-07T06:17:55.827Z 

Is there a simple way than using big functions ?

like image 470
sandy Avatar asked May 13 '13 06:05

sandy


People also ask

What date format is TZ?

The T is just a literal to separate the date from the time, and the Z means “zero hour offset” also known as “Zulu time” (UTC). If your strings always have a “Z” you can use: SimpleDateFormat format = new SimpleDateFormat( “yyyy-MM-dd'T'HH:mm:ss).

How do you convert date to UTC format?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

How do I change TimeZone to date?

Calendar calendar = new GregorianCalendar(TimeZone. getTimeZone("GMT")); DateFormat formatter = new SimpleDateFormat("dd MMM yyyy HH:mm:ss z"); formatter. setTimeZone(TimeZone.


2 Answers

Try this:

 date("Y-m-d\TH:i:s.000\Z", strtotime("2013-05-07 18:56:57")); 
like image 98
Eugene Avatar answered Sep 20 '22 05:09

Eugene


This should give the proper ISO8601 date/time in Z(ulu) timezone:

str_replace('+00:00', 'Z', gmdate('c')) 

To do a date conversion:

str_replace('+00:00', 'Z', gmdate('c', strtotime('2013-05-07 18:56:57'))) 

To have the additional .000 (which is useless imho):

str_replace('+00:00', '.000Z', gmdate('c', strtotime('2013-05-07 18:56:57'))) 
like image 24
2 revs Avatar answered Sep 18 '22 05:09

2 revs