Convert timestamp into 2016-12-24T14:11:00Z - php

Hi I'm trying to convert my result out of a timestamp field into a formated way.
So from '2016-12-24 14:11:00' to '2016-12-24T14:11:00Z'
What is the easiest way to do so?

It looks like ISO 8601 format which can be achived by date with c param.
echo date('c', strtotime('2016-12-24 14:11:00'));
but if u want exactly that format you could personalize it
echo date('Y-m-d\TH:i:s\Z', strtotime('2016-12-24 14:11:00'));
If z letter means something then probably you need to find proper parameter on http://php.net/manual/en/function.date.php

SELECT concat(replace(`date_field`,' ','T'),'Z')

Related

How do get this date/time format?

I saw a date/time format in a PHP code that has something like this:
2015-06-19T00:38:04Z
How can I generate a datetime having that kind of format?
I know this kind of format date("Y-m-d H:i:s"); outputs different 2015-06-19T00:38:04Z because it has a T and a Z.
So it's like I want to get the current date/time having a T and a Z
Escape T and Z in format
echo date("Y-m-d\TH:i:s\Z"); // 2015-06-17T16:50:50Z
UPDATE Thanks to Lithis. To take a GMT/UTC time
echo gmdate("Y-m-d\TH:i:s\Z"); // 2015-06-17T17:06:06Z

PHP format date from returned data

I have a query that is returning a grid. One of the columns brings back a column with a date, like this:
echo "<td>{$Row[ETA]}</td>";
This displays the ETA from the database like this:
2013-10-30 20:00:0
I basically want to remove the TIME portion and just keep the date. Can this be done in the TD or do I have to the conversion elsewhere? I would like to just do the conversion within the cell, if possible.
Let me know how this can be done.
You can use the strtotime() and date() functions to achieve this!!!
date("Y-m-d", strtotime($Row[ETA]));
Well! if you want to get just date then you should use this function in your query
DATE(date_field)
as this will return only date from the datetime column
Ideally you should listen to the suggestion by JohnConde because it will limit your overhead between the database and your script but you can also substr() on the fly if you wish like this:
echo "<td>".substr($Row['ETA'], 0, 10)."</td>";
You have to echo it differently:
$eta = $Row['ETA'];
$etaDate = date("Y-m-d", strtotime($eta));
//now use $etaDate
You can use the date function to format the time, as what you're getting is a date as a string, you can use strtotime.
I think the format you're looking for is: date("Y-m-d", strtotime($Row["ETA"]));, you can either parse that into a variable and save it there, or you can concatenate the results together for the final string.

Formatting datetime

i am trying to format a datetime which comes fromt he database in the format of
2012-06-11 21:39:54
However i want it to display in the format of June 11
How can do this?
Thanks
echo date('M d', strtotime('2012-06-11 21:39:54'));
Output
You can also use DateTime object.
$date = new DateTime($yourString);
$date->format($yourFOrmat);
I think that it would be the best way because DateTime is really more powerful than timestamp and date/strtotime functions.
From the code I gave above you can add functionalities like modifying dates, iterate over the time, compare 2 dates without functions like str_to_time...
$date->modify('+1 day');//the day after for example
foreach(new DatePeriod($date,new DateInterval('PT1M'),10){
$date->format($yourFormat);//iterate each minute
}
and so on
PHP manual gives an excellent documentation about using Date/Time functions. Basically you will need a combination of two functions: strtotime() and date().
strtotime() will convert your date into Unix timestamp which can be supplied to date() as second argument.
The format of date you will need is: M d.
Alternative: In addition you could also try the MYSQL counterpart which won't require conversion to UNIX timestamp. It is documented here. Assuming you are using date as your Datetime field, you will need something like this,
SELECT id,..,DATE_FORMAT(`date`, '%M %d') as f_date FROM table
For formatting date using php, you need to pass timestamp of date
and format specifiers as arguments into date function .
Eg echo date('M d',strtotime('2012-06-11 21:39:54'));

php date format help

I am having a little trouble with changing the format of a date with php, the format in question is dd/mm/yy, however I wanting to change that to yyyy-mm-dd.
I have tried doing this,
date('Y-m-d', '23/04/10')
The second argument for date() should be unixtimestamp and not a string like you have. Use strtotime() (you might need to change the string, read up on the manual!) to convert your string to unixtimestamp first.
date('Y-m-d', strtotime('23/04/10'))

CodeIgniter: format mysql DATETIME field dd/mm/yy hh:mm

Hi pretty much what it says on the tin.
I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.
I've tried:
<?php
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring,$row->created);
?>
But I'm getting an error:
Message: A non well formed numeric value encountered
Any help most appreciated!
Cheers,
Billy
Try:
echo date ("d/m/Y h:ia",strtotime($row->created));
The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));
The only difference between mdate() and date() is, as the CodeIgniter docs say:
This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.
The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.
Got this to work using treeface's solution, with one minor change:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created));
//strtoDATE didn't work but strtoTIME did
Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.
HTH
I'm using:
mdate(date_string,mysql_to_unix($row->created))
That should work.

Categories