Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do strtotime(' ') and strtotime('.') return a timestamp?

Tags:

php

strtotime

After some messing around with strtotime() in PHP I noticed that it gives a valid timestamp back when you pass in spaces and or dots.

var_dump(strtotime(" "));
var_dump(strtotime("."));
var_dump(strtotime(". .. .. .. .... .. . .. ."));

produces:

int 1443009652
int 1443009652
int 1443009652

Why does PHP see this as valid?

like image 889
JurrieG Avatar asked Sep 23 '15 12:09

JurrieG


People also ask

What does Strtotime return?

Definition and Usage 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 is a timestamp in PHP?

Description ¶ time(): int. Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.

How do I change Strtotime?

Code for converting a string to dateTime$date = strtotime ( $input ); echo date ( 'd/M/Y h:i:s' , $date );

How do you convert string to time in python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.


1 Answers

The simplest answer is some of them are falsey

var_dump(DateTime(false)); // date shown is current time

My bet is that the parser (which is trying to clean up a wide variety of acceptable inputs) strips the periods out (that are not being used as a delimiter), leaving only an empty string. It's the only explanation that makes sense.

echo strtotime('1.1.2000'); // outputs 946681200
like image 193
Machavity Avatar answered Oct 03 '22 07:10

Machavity