UTC to local time adjustment woes with PHP [duplicate] - php

This question already has answers here:
Timezone conversion in php
(9 answers)
Closed 8 years ago.
Can someone explain why the following doesn't work?
I have a PHP script that runs on a shared hosting account. The script should output my local time (for the time zone where I'm located, i.e. GMT-8:00, or PST.)
I'm reading documentation for time() PHP method that is supposed to return a Unix timestamp in UTC (or GMT). OK, good. So I need to subtract 8 hrs to get my local time, right?
So I'm doing this:
echo(date("n/j/Y, g:i:s A", time() - 28800)); //8hrs = 28800 seconds
But I'm getting time that is 5 hrs behind!
What can be wrong in a one-line statement as such?
PS. Sorry, if I'm asking the obvious. I'm coming from the world of .NET. I'm new to PHP.

PHP's time() function will always return seconds since the epoch. The culprit for getting unexpected behavior is actually the date() function.
PHP's date() function will automatically convert the date to the current default timezone of PHP, which must be GMT - 3. This is why when you subtract 8 hours you're 5 hours behind.
Instead of using date(), you probably want to check in to gmdate(). Something like:
echo(gmdate("n/j/Y, g:i:s A", time() - 28800));
may be what you need.

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.

Date() returns a year in future instead of correct year

date('Y-m-d H:i:s','1345453380000'); should return 2012-08-20 09:03:00 but instead it returns 44605-09-21 02:00:00
I understand the time difference of one day may be due to me not specifically setting timezone in the conversion, but 38k years in the future is a bit off, where am I doing it wrong?
Is it the trailing zeros?
I appreciate any pointers... (BTW That timestamp is how certain apps deliver them, I did not craft it myself)
When I getdate() that same timestamp the same issue happens, so I don't think my code is wrong, rather something is problematic with the trailing 0's...
But even if I use the from human to timestamp converted, with epoch converter, I get wrong results.
ONLY if I remove ALL zeros it seems to return a proper date.
Why?
Note again, the timestamp is how it comes from an online "diary" App, and Epoch converter IS able to read it!
(https://www.epochconverter.com/)
What is happening here is that the timestamp is in milliseconds, but PHP expects seconds. Epoch converter works fine with both seconds and milliseconds. What you have to do is call date('Y-m-d H:i:s', ($timestamp/1000));
Look at this demo.
date('Y-m-d H:i:s',(1345453380000/1000));
Above code converts milliseconds to seconds.

Javascript and PHP timestamps different, giving different time [duplicate]

This question already has answers here:
Timestamp between Javascript and PHP
(3 answers)
Closed 9 years ago.
I'm getting different values for timestamp using javascript and PHP when code is executed almost at the same time.
using Javascript Date.getTime(), i get
1375228800000
for timestamp while at the same time PHP reports
1375233890
as timestamp from a call to time().
The first ten digits are close but the timestamp from javascript has extra 3 digits (000) which I think is causing PHP to return the date as 1996-08-05 17:08:40 instead of 2013-08-31.
I'm running Javascript and PHP on the same machine.
Why is Javascript adding extra digits (000) and how can I solve this problem? Please help.
JavaScript measures time in milliseconds, not in seconds. Just divide by 1,000.

Converting client side time to UTC on the server for storage [duplicate]

This question already has answers here:
how to convert php date formats to GMT and vice versa?
(3 answers)
Closed 9 years ago.
This may be a duplicate but I can't seem to find what I'm looking for.
I want to store dates given by the client in UTC. I have a javascript library that detects the client's timezone and I'm sending the timezone name (e.g. America/Halifax) to the server with the rest of the form data.
I've been searching now for the php functions that I can use to take the date entered and the timezone and convert it to UTC.
I think gmdate is the correct function but how do I use the date entered and the timezone with gmdate?
Try setting date_default_timezone_set('America/Halifax') at the top of your script.
http://php.net/manual/en/timezones.php

convert date to time stamp by php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Unix timestamp before 1970 (even before 1700), using PHP
as you know we have date element in HTML5,It can return something like it 1000-10-05,now I need to make this as time stamp,I try to do it by mktime() but It doesn't return true value.
now How can I do that?
mktime() is timestamp based. On 32 bit systems, timestamps can't reach dates that far back - a signed int can reach from ca. 1900 to 2038.
If you need to do operations with pre-1900 dates, consider using the DateTime library instead, available in PHP 5.2 and newer. It works with 64-bit data internally and can manage any date.
use
strtotime($yourHTML$DateString);
If your problem is not the timestamp range issue as discussed, try strtotime instead of mktime.
strtotime('1000-10-05') must do it. but it supports only 1970 and >

Categories