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.
Related
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.
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have some problems converting an HTML date to a MySQL date.
I have a form with an input field (type="date"), which outputs a date in format 29/04/2015
Whenever I click on the Send button, I send the date via PHP to a database which has a column in DATE format (2015-04-29)
Unfortunately the result is that the date is always stored as 1970-01-01, which means the conversion didn't work out as expected.
My conversion code, right now, is:
#$entryDate = str_replace("/", "-", $_POST['entryDate']);
#$entryDate = date('Y-m-d', strtotime($entryDate));
Do you have any idea why the conversion is not working?
Thanks a lot for your help
HTML5 is not truly/fully supported in all current browsers.
But if you can step over that, the input method gives you a date formatted in "YYYY-MM-DD" format (regardless of the localization of the display order in the representation to the user in the browser), no need to convert it to use in mysql at all. But do validate it as a valid input, esp. given the lack of support in some browsers (and for once it's not just IE, Firefox also has issues with this one).
See also Is there any way to change input type="date" format?
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
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.
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 >