I'm trying to use timeago ( http://timeago.yarp.com/ ) and have found solutions for converting timestamps from MYSQL using php to ISO-8601.
date('c',strtotime($TimeStamp));
This works fine except im getting the timezone offset at the end
2011-07-10T08:46:50-**05:00**
what I want is 2011-07-10T08:46:50Z
Does anyone have a solution or know why i'm getting the timezone offset?
You can do it directly in MySQL:
SELECT DATE_FORMAT(yourfield, '%Y-%m-%dT%H:%i:%s0Z')
There is a GET_FORMAT(datetime, 'iso') call as well, but that returns the format string for ISO 9075, which is not quite what you want. Since it doesn't do 8601 directly, you have to build the format string yourself.
The 'c' will return the entire date, including the timezone offset. You will have to build it manually using the other options. Try this:
$time_stamp = time();
echo date('o-m-N',$time_stamp)."T".date('H:i:s',$time_stamp)."Z";
http://php.net/manual/en/class.datetime.php#datetime.constants.types
const string ISO8601 = "Y-m-d\TH:i:sO";
You can use your own format:
date('Y-m-d\TH:i:s\Z',strtotime($TimeStamp));
Simple for Open Graph
date('Y-m-d\TH:i', strtotime('2015/09/24 08:46:50))
Another way:
substr(date('c',strtotime($Timestamp)),0,-6).'Z';
Related
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.
I need to display and handle UTC dates in the following format:
2013-06-28T22:15:00Z
As this format is part of the ISO8601 standard I have no trouble creating DateTime objects from strings like the one above. However I can't find a clean way (meaning no string manipulations like substr and replace, etc.) to present my DateTime object in the desired format. I tried to tweak the server and php datetime settings, with little success. I always get:
$date->format(DateTime::ISO8601); // gives 2013-06-28T22:15:00+00:00
Is there any date format or configuration setting that will give me the desired string? Or I'll have to append the 'Z' manually to a custom time format?
No, there is no special constant for the desired format. I would use:
$date->format('Y-m-d\TH:i:s\Z');
But you will have to make sure that the times you are using are really UTC to avoid interpretation errors in your application.
If you are using Carbon then the method is:
echo $dt->toIso8601ZuluString();
// 2019-02-01T03:45:27Z
In PHP 8 the format character p was added:
$timestamp = new DateTimeImmutable('2013-06-28T22:15:00Z');
echo $timestamp->format('Y-m-d\TH:i:sp');
// 2013-06-28T22:15:00Z
In order to get the UTC date in the desired format, you can use something like this:
gmdate('Y-m-d\TH:i:s\Z', $date->format('U'));
To do this with the object-oriented style date object you need to first set the timezone to UTC, and then output the date:
function dateTo8601Zulu(\DateTimeInterface $date):string {
return (clone $date)
->setTimezone(new \DateTimeZone('UTC'))
->format('Y-m-d\TH:i:s\Z');
}
Edit: clone object before changing timezone.
Since PHP 7.2 DateTimeInterface::ATOM was introduced in favor of DateTimeInterface::ISO8601, although it still lives on for backward compatability reasons.
Usage
$dateTimeObject->format(DateTimeInterface::ATOM)
I've been using setTimeStamp to convert a Unix Timestamp to a datetime in the following way:
$startHireConverted = strtotime($startHire); // converts start hire to time
$endHireConverted = strtotime($endHire); // converts end hire to time
$startdt = new DateTime();
$startdt->setTimeStamp($startHireConverted);
$mysql_startdate = $startdt->format("Y-m-d H:i");
This was working nicely, but recently I've put the website live and the version of PHP can only be 5.2.12 which doesn't support the setTimeStamp method.
I've tried changing setTimeStamp to format which is getting rid of the errors and converting the datetime but it is changing it to the current datetime - 5 hours for some reason rather than the date stored in $startHire.
$startdt->format($startHireConverted);
Any ideas on how to get around this problem?
$startHire starts out as a string version of datetime.
Thankyou
I don't think you need to do the step with unix timestamp at all:
$startdt = new DateTime($startHire);
PHP manual: DateTime::__construct
But that might depend on the format you're getting $startHire in. See Supported Date and Time Formats.
well, I"ve got a time format of the type: 2011-02-16T01:25:50+04:00
I want to strip the +04:00 from the end of the string. How to do it?
I am converting a timestamp from mysql to ISO 8601 format using this function:
$timeagotime = date('c',strtotime($row['created_at']));
where $timeagotime contains the value: 2011-02-16T01:25:50+04:00
Refer to the documentation for php's date function: http://php.net/manual/en/function.date.php
You need to use a different format string. For instance,
date('Y-m-d',strtotime($row['created_at']));
Givens you "2011-02-16". There are a number of format strings... check out the doc.
You could use substr() to remove it, or just generate a date without it in the first place, though you couldn't use the 'c' formatting option, since that'd just add the timezone on again.
$timeagotime = substr($timeagotime, 0, 19)
$timeagotime = date('Y-m-dTG:i:s', strtotime(...));
If you absolutely don't need the end then you can use the following. Otherwise I'd suggest using a different way to generate your date from the table. PHP Date
$timeagotime = substr(date('c',strtotime($row['created_at'])), 0, 19);
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.