time() to DateTime - php

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).

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 date to UTC in PHP

I have this date from my local timezone "2019-07-27T02:00:00"
I'm trying to save it to the database in a UTC timezone, so I can deal with it later and convert to to other timezone from UTC,
But when I do:
$date = new \DateTime("2019-07-27T02:00:00");
I got a:
DateTime #1564192800 {#519
date: 2019-07-27 02:00:00.0 UTC (+00:00)
}
Whatever I set here,
new \DateTime("2019-07-26T06:00:00")
I still get a UTC timezone, so this example
$date = new \DateTime("2019-07-26T06:00:00");
Will get me a result of:
DateTime #1564120800 {#519
date: 2019-07-26 06:00:00.0 UTC (+00:00)
}
It's like the timezone of the date (2019-07-26T06:00:00) is already in UTC? But it's not (?).
So, converting it to UTC with
$date->setTimezone(new \DateTimeZone('UTC'))
has no affect at all.
PHP probably thinks you're already in UTC, and accepts any DateTime object that doesn't have timezone explicitly listed as UTC as well.
You can check what PHP thinks is your timezone by looking in your php.ini file, or by using the command echodate_default_timezone_get(). Similarly, you can edit your timezone globally by editing your php.ini file or using date_default_timezone_set().
This is a list of timezones PHP can use.
As a side note: though it might seem like a good idea to keep your global timezone set to UTC and just use a DateTime/DateTimeZone to set local variables to the correct offset, don't do it. It'll affect the timestamps of your logs, and can lead to some very painful gotchas.

How can I change the timezone of a date string?

I would like to change the timezone of a time like:
2015-08-24 01:30:40
so that it's in the America/Los_Angeles timezone, but the code below isn't working as expected:
$timedate = date_timezone_set(
date_create_from_format('Y-m-d h:i:s A', $row[timezone]),
new DateTimeZone('America/Los Angeles')
);
$row[timezone] successfully returns 2015-08-24 01:30:40 on it's own, but when I do it like this to try to change the timezone, it doesn't work.
All PHP code after that doesn't run. I have done lots of searching, but I can't figure out how to get this to work properly so can someone tell me what I am doing wrong?
Your call to date_create_from_format includes an A at the end of the format string - that looks specious to me, given that your value doesn't end with am or pm. Your use of h looks unlikely to be correct too, as that's for a 12-hour hour-of-day, which isn't useful when you don't have an am/pm indicator. I suspect you want a format of
Y-m-d H:i:s
That appears to match your sample string better...
Additionally, as noted in comments, your time zone ID should be 'America/Los_Angeles' rather than 'America/Los Angeles'.
I would also separate the various calls you have in this single large line of code, to make it easier to diagnose - that way you could tell that it's the parsing that's failing, rather than anything to do with the time zone. You have three separate operations here:
Parse a string to a datetime
Create a time zone object
Set the time zone in the datetime
Keeping those three in separate statements will make the code easier to read and easier to maintain. (In particular, you'd be able to find and correct these two problems independently...)

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.

Zend Date MySQL Timezone offset problem

I've got a site which uses the Zend Framework. There's a form which the users fill in, including a Date field. Currently I'm using this to create a new Zend_Date object and then getting the date in ISO format to put into the MySQL database. However when the date is returned in ISO format it also has the timezone offset appended to the end (e.g. 2011-01-01T00:00:00-0500), which MySQL doesn't like. When I try to add it to the DB it gives me an invalid date error. I'm sure there must be a simple solution to return the date without the timezone offset, but I can't seem to find it.
Any suggestions?
Thanks.
if you toString your Zend_Date object and then use the mb_strcut() function to remove the timezone and then insert it into the db it should be fine ?
another way would be to alter the Zend function that returns the date in an ISO format to prevent it from appending the timezone to the end of the date.

Categories