Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php get timestamp from string when i know the format

Is there a php func that does this:

$timestamp = get_timestamp_from_str('d/m/Y H:i', '10/10/2012 10:10');

strtotime() will use the USA version for '10/10/2012' wich is m/d/Y, but i have the day first.

For a particular case i can make my own parser, but the date format can change depending on the visitors local settings. However, i will always know the current format.

I had to insert this last paragraph so that this question is long and good enough for this portals question quality filter

Thank you

like image 836
Skacc Avatar asked Apr 23 '12 21:04

Skacc


People also ask

How check date format is correct or not in PHP?

PHP | checkdate() Function The checkdate() function is a built-in function in PHP which checks the validity of the date passed in the arguments. It accepts the date in the format mm/dd/yyyy. The function returns a boolean value. It returns true if the date is a valid one, else it returns false.

How does Strtotime work in PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

What does Strtotime return?

Return Values PHP strtotime() function returns a timestamp value for the given date string. Incase of failure, this function returns the boolean value false.

What PHP function is use to display a formatted date?

The PHP date() function convert a timestamp to a more readable date and time. The computer stores dates and times in a format called UNIX Timestamp, which measures time as a number of seconds since the beginning of the Unix epoch (midnight Greenwich Mean Time on January 1, 1970 i.e. January 1, 1970 00:00:00 GMT ).


1 Answers

Yes, it's called DateTime::createFromFormat().

You would use it as:

$datetime = DateTime::createFromFormat( 'd/m/Y H:i', '10/10/2012 10:10', new DateTimeZone('Something'));
$timestamp = $datetime->getTimestamp();

Just make sure your timezone string is in the list of supported timezones.

like image 164
nickb Avatar answered Nov 14 '22 21:11

nickb