Convert json date format from one timezone to another in php - php

While using google api for feed, I get blog post's creation time in format - 2015-04-11T06:33:00.001-07:00 which is datestamp for another timezone. I want to convert datestamp to IST timezone (eg. 2015-04-12T09:51:00.001+05:30) with php. How do I do that?
In another question what all I could find is convert 2015-04-12T09:51:00.001 to another format, that's it. I am unable to convert it to mentioned format.

This can be easily accomplished with DateTime:
$dt = new DateTime('2015-04-11T06:33:00.001-07:00');
$dt->setTimeZone(new DateTimeZone('Asia/Colombo'));
echo $dt->format('c');
demo

Related

Convert From MediaWiki Time Format To Unix Timestamp In PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 15 days ago.
MediaWiki (the free software behind Wikipedia) stores database timestamps in a unique binary(14) format for fields of the database. This is described further in their timestamp documentation.
The format of timestamps used in MediaWiki URLs and in some of the
MediaWiki database fields is yyyymmddhhmmss. For example, the
timestamp for 2023-01-20 17:12:22 (UTC) is 20230120171222. The
timezone for these timestamps is UTC.
I have also seen a similar timestamp format in other places such as URLs for the Internet Archive. I am regularly needing to compare these timestamps against timestamps which are stored in a standard Unix timestamp format (seconds from the Unix epoch). I believe this should be a common format so it surprises me that I can't find a ready-made solution to easily convert from the MediaWiki format to a Unix timestamp.
What I'm most interested in is the best way to do this conversion. That is:
Relatively short/simple to understand code.
Most efficient algorithm.
Does detect errors in original format.
There is apparently a function that MediaWiki includes for conversion named "wfTimestamp" however I haven't been able to locate this function itself or the source code online and I understand it has a large number of unnecessary features beyond the simple conversion. One potential solution may be to remove other parts of that function, but I still don't know if that function is the optimal solution or if there's a better way. There are lots of questions on the more general conversion to timestamps but I'm hoping for something specific to this format. I've thought of a lot of ways to solve it such as a regular expression, mktime after string split, strtotime, etc... but I'm not sure which will be fastest for this particular task/time format if it had to be done a lot of times. I am assuming since this format exists in at least two places, an optimal solution for this specific format conversion could be useful for others as well. Thanks.
I think this is what you're probably looking:
$timestamp = strtotime("20230120171222");
// 1674234742
The Unix timestamp that this function returns does not contain information about time zones. In order to do calculations with date/time information, you should use the more capable DateTimeImmutable.
Please see here: https://www.php.net/manual/en/function.strtotime.php
You can use DateTime::createFromFormat function with specified format.
$date = DateTime::createFromFormat("YmdHis", "20230120171222", new \DateTimeZone('UTC'));
$timestamp = $date->getTimestamp();
I'm not sure that you can find more optimised way, because even if you will parse this manually, you have to consider that there are leap years and not every day has exactly 24 hours. PHP does it for you.
In order to interpret the string "20230120171222" as UTC time, the time zone must be specified with strtotime or the default time zone must be set to UTC.
$dateStr = "20230120171222";
$timestamp = strtotime($dateStr.' UTC');
var_dump($timestamp); //int(1674234742)
See this example for comparison.

Converting YYYY-MM-DDTHH:mm for Google Calendar [duplicate]

This question already has answers here:
How to convert ISO8601 to Date format in php
(6 answers)
Closed 2 years ago.
I'm currently having trouble figuring out how to convert the current timestamp to one that works as a parameter for google calendar. I'm using PHP.
Right now the format is YYYY-MM-DDTHH:mm. The date I'm trying to convert is:
2020-03-11 10:00:00
So far I've tried using gmdate and passing in the current date with strtotime but I can't figure out how to make it the right format.
Thanks for any help.
you can do in php like:
date('your needed format here', strtotime($yourtime))
check https://www.php.net/manual/en/function.date.php for format detail. that will give you the needed string.
date() will convert unix time format in given format, the strtotime() makes unix time out of almost any time format string, it will detect the format automatically.

Which kind of date format is this and how to convert it into JavaScript date?

Does this date format have any particular name and/or is it used in any particular programming language?
2012-11-28T12:52:22+0000
I need to convert this into JavaScript or PHP date, possibly without using string concatenation.
Kind of what somebody did here: Convert a Unix timestamp to time in JavaScript
Does any of these 2 programming languages have any pre-made function to do so?
PHP handles this easily with the DateTime class:
$dt = new DateTime('2012-11-28T12:52:22+0000');
echo $dt->format('Y-m-d');
See it in action
format() accepts the same parameters as date() so you can format it into any format you need.
Reference
DateTime
date()
That's an ISO-8601 date, which is becoming very common on the web.
It is an interoperability format, supported by many frameworks, and was added to PHP in version 5.1.0.
In JavaScript, it is supported by the Date.toISOString() method, and libraries such as Moment.js - among others.
You can construct a JavaScript Date from an ISO string using its constructor:
var date = new Date('2012-11-28T12:52:22+0000');
Be aware that JavaScript is notoriously bad with dates. It only understands dates from a local perspective, or from UTC. Any offset you provide will be merged into the date. That is being worked on in Moment.js. see here.
Yes, it's called an ISO-8601 date. With PHP you generate it with date('c') and with JavaScript:
var date = new Date('2012-11-28T12:52:22+0000');
date.toISOString();
As you can see, the argument to Date() can be just such a string.

What date format is this? /Date(-62135575200000-0600)/

I'm consuming an API (XML output) with PHP and it is returning all timestamps in the following format. Believe it or not this isn't in the docs anywhere.
Is this a standard date format?
What format is this timestamp in?
How can I convert it to a unix timestamp in PHP?
/Date(-62135575200000-0600)/
It's the default JSON format of date.
Looks like a .NET Date string, which would mean that it is milliseconds since 1970, and the timezone.

time() to DateTime

I have an application that uses time() to record the time a topic was posted. I have done this for a long time and the only glitch i ever had with the method was that the time was always off by an hour (mainly a DST issue i never looked into).
I'm want to switch to the DateTime method, since I'm also switching to Twig, which uses that date format when setting a timezone.
But from what I can see, you can't use timestamps to parse the date. my question is, how do you input a date and parse it and what format are they looking for if it isn't time()?
By the sounds of it your server's default timezone is UTC. I suggest you try changing the default_timezone setting in php.ini to something that applies DST. This will mean that when you use date(), the resulting output will be correctly adjusted for DST.
you can't use timestamps to parse the date
There is a way, if your php version is >=5.3 :
$time = time();//or other time
$date = DateTime::createFromFormat('U',$time);
but as a matter of fact it is somewhat weird to use timestamp as DateTime does not rely on it. You can parse a date string directly with the datetime constructor if your date is english or "french" (i.e d/m/y). You can also create your custom format.
how do you input a date and parse it and what format are they looking for if it isn't time()
if it is a a standard format date :
$date = new DateTime($yourString);//would throw an exception if the format is wrong
if it is a custom format, like DD/MM/YYYY 14h32mins
$date = DateTime::createFromFormat('d/m/Y h\hi\m\i\n\s',$yourString);
if($date === false){//if format does not fit
exit('Your wrong !');
}
I have an application that uses time() to record the time a topic was posted.
Very bad practice. In MySQL you have DateTime format (and each relational data base system has his own equivalent) to store date times and there are fully compatible with the DateTime constructor in PHP so migrate to it (use FROM_TIMESTAMP to change into DateTime).

Categories