convert date to time stamp by php [duplicate] - php

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 >

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.

UTC to local time adjustment woes with PHP [duplicate]

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.

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

PHP: parsing a date of any format (especially: 2008-10-20, 2008/10/20, 2008.10.20, Nov. 20/08)

strtotime("25/03/1957") returns false. what will satisfy all of these date formats? i can't imagine how long it would take to actually make my own, so i hope there's already one out there you know of.
thanks!
Considering some dates are valid but can point to two different actual dates, no function will ever be able to "guess" the right format at all times...
To help with that, with PHP >= 5.3, a new function has been added : date_create_from_format -- but it doesn't exist with PHP < 5.3, unfortunately...
(See also DateTime::createFromFormat)
Still, in the example you took, the year 1957 is a possible source of problems : PHP generally works with UNIX Timestamps, when it comes to dates...
And, at least on 32-bits systems, those can only represent dates between 1970 and 2038 -- as they count the number of seconds since 1970-01-01.
To avoid this problem, it's often a good idea to use the DateTime class, with which (quoting) :
The date and time information is
internally stored as an 64-bit number
so all imaginable dates (including
negative years) are supported. The
range is from about 292 billion years
in the past to the same in the future.
(It will not solve the parsing problems with PHP < 5.3 ; but it'll solve the date-range problem...)
I've found that dateTime objects support a wider range of formats than the strtotime() function, and the timezone settings of your server also make a difference; but I ended up building a function that would replace '/' with '-' before using the string to date methods. I also test for valid, then try swapping the apparent dd and mm (25-03-2001 => 03-25-2001) if invalid before testing again.

Categories