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

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?

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

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

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

DATE_ATOM is provided for this format:
$theStart_date = date(DATE_ATOM, strtotime($start_date));
Output:
2013-04-10T09:10:30-04:00

Related

Convert Now and Ten Minutes Ago to Different Timezone

I would like to convert the timestamps from ten minutes ago and now to match the following format:
2018-09-23T04:47:07.237
Here are the timestamps I'd like to convert to match the above format:
$now = date('m/d/y g:i a');
$now = strtotime($now);
$ten_minutes_ago = strtotime('-10 minutes');
How can I do this? Thanks!
Use date_format function instead. You don't need to convert to UNIX timestamp using strtotime function. Instead use DateTime library
Check the following (Rextester Demo):
$now = new DateTime(); // create a datetime object
$sub = new DateInterval("PT10M"); // Interval of 10 mins
$ten_minutes_ago = new DateTime();
$ten_minutes_ago->sub($sub); // subtract 10 minutes
// changed formats
$now_changed = date_format($now, DATE_ISO8601);
$ten_minutes_ago_changed = date_format($ten_minutes_ago, DATE_ISO8601);
// print output
echo $now_changed; //2018-09-23T02:58:25-0400
echo $ten_minutes_ago_changed; // 2018-09-23T02:48:25-0400
Details:
The date_format() function returns a date formatted according to the specified format.
DATE_ISO8601 - ISO-8601 (example: 2013-04-12T15:52:01+0000)
You can check for more formatting options here.
Here is how I would do what you are asking for.
If your data is in a string. Here is the only line of code you need:
$date = date('m/d/y g:i a'); //Gets a date string.
echo substr(date('Y-m-d\TH:i:s.u', strtotime($date . ' -10 minutes')), 0, -3); // PHP < 7.0.0
//echo date('Y-m-d\TH:i:s.v', strtotime($date . ' -10 minutes')); //PHP > 7.0.0
This will produce:
Ex.
09/23/18 12:13 am
To
2018-09-23T00:03:00.000
One thing to note here. The microseconds will always be zeros if your original input date is a string and in the format m/d/y g:i a that you have specified. The reason being is that there is no millisecond information to be had from the date string.
If you create you input date as a dateTime object, the object will be able to keep track of the microseconds for you.

How i can reproduce this format?

How i can reproduce this date format with php?
2016-04-07T09:03:32
I'm using:
$date = date('Y-m-d h:i:s', time());
But i don't know what is The 'T' printed beetween date and time
What youre looking for is:
$date = date('Y-m-d\Th:i:s', time());
The 'T' is for HTML5 local date-time required format.
If you want to use this format to be sure of compatability, you should use the constant to speficy the format which includes the timezone offset between local and GMT.
$date = date(DateTime::ATOM, time());
which will output:
2016-04-07T01:20:48-07:00
The T designates the start of a time string in the ISO 8601 date format.
You can reproduce this format using date like this:
date('c')
But note this will (and should) also include the timezone offset. Because the date string will always be the same format you remove this quite easily to match your specified format with substr like so:
$date = date('c');
$formattedDate = substr($date, 0, 19);

Convert time with format HH:MM AM/PM to HH:MM:SS

I am use PHP and MySQL. How can I convert time with format HH:MM AM/PM to HH:MM:SS to save time to MySQL?
Use strtotime to convert it to a timestamp and then format that using the date function.
Example below assumes $time has been set to the time you need to format.
$time = date( "H:i:s", strtotime( $time ) );
The way I would do it is:
<?php
$date = "6:23 AM";
$timeType = explode(" ", $date);
$timeItems = explode(":", $timeType[0]);
if($timeType[1] == "PM"){
$timeItems[0] += 12;
}
$time = implode(":", $timeItems);
$time .= ":00";
var_dump($time);
?>
Use date_format() to resolve your problem:
DATE_FORMAT(date,'%H:%i:%s')
Easy. In MySQL use DATE_FORMAT like this:
SELECT DATE_FORMAT(NOW(), '%H:%i:%s');
And using your example you could couple this with STR_TO_DATE like this:
SELECT DATE_FORMAT(STR_TO_DATE('09:41 AM', '%h:%i %p'), '%H:%i:%s');
Then in your MySQL query you could substitute the 09:41 AM with a variable based on your MySQL table.

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

/Date(1341788400000+0100)/ to DD/MM/YYYY HH:MM

I have several dates being outputted into variables. They are formatted as follows:
/Date(1341788400000+0100)/
How would I go about formatting them using PHP into:
DD/MM/YYYY HH:MM
Thanks!
I ended up using the following, as the initial format was in milliseconds:
$date = 1341788400000+0100;
$date = ( $date / 1000 );
$date = date("d/m/Y H:m", $date);
$date = 1341788400000+0100;
echo date("Y/m/d H:m",$date);
Unless the +0100 is the actual time of the day (01:00) ?
First, you parse it, e.g. using strtok() http://php.net/manual/en/function.strtok.php
Then parse it as a number.
$seconds = intval($a)
Then format it using
date("Y/m/d H:m", $seconds)`.

Categories