Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format timestamps with embedded `T` character?

I need to format a timestamp in ISO 8601 format (e.g. 2001-10-26T21:32:52). When I use the date() function in PHP, it replaces T with the Timezone (as it is supposed to do).

The command I'm using is:

$time = date("y-m-dTH:i:s", time());

This produces: 10-02-13EST10:21:03

How do I get it to insert an actual T and not replace with EST?

like image 993
pgtips Avatar asked Feb 13 '10 15:02

pgtips


People also ask

What is TIMESTAMP format with T and Z?

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

What is TT TIMESTAMP?

The tt is part of the Custom Date and Time format strings. tt is for the AM/PM designator.

How do you format a date TIMESTAMP?

The default format of the timestamp contained in the string is yyyy-mm-dd hh:mm:ss. However, you can specify an optional format string defining the data format of the string field.

How do I change the format of a TIMESTAMP in SQL?

select to_char(sysdate, 'YYYY-MM-DD') from dual; To get this format by default, set it in your session's NLS_DATE_FORMAT parameter: alter session set NLS_DATE_FORMAT = 'YYYY-MM-DD'; You can also set the NLS_TIMESTAMP_FORMAT and NLS_TIMESTAMP_TZ_FORMAT .


4 Answers

Your format shoule be : "c"

$time = date("c", time());

From PHP manual:

Format Descriptions                        Example
c      ISO 8601 date (added in PHP 5)   2004-02-12T15:19:21+00:00
like image 133
Patrick Desjardins Avatar answered Oct 14 '22 21:10

Patrick Desjardins


If you need to insert a character which should not be interpreted, precede it with a backslash:

$time = date("y-m-d\TH:i:s", time());
like image 42
Matteo Riva Avatar answered Oct 14 '22 22:10

Matteo Riva


You could format the date and time parts seperately, then concatenate the two parts with "T":

<?php
 $time = time(); 
 $time = date( "y-m-d",$time )."T".date( "H:i:s", $time );
?>
like image 39
GeekTantra Avatar answered Oct 14 '22 22:10

GeekTantra


DATE_ATOM is provided for this format:

$theStart_date = date(DATE_ATOM, strtotime($start_date));

Output:

2013-04-10T09:10:30-04:00
like image 1
Rohit Dubey Avatar answered Oct 14 '22 23:10

Rohit Dubey