Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a unix timestamp from PHP date time?

I'm trying to get a unix timestamp with PHP but it doesn't seem to be working. Here is the format I'm trying to convert to a unix timestamp:

PHP

$datetime = '2012-07-25 14:35:08';

$unix_time = date('Ymdhis', strtotime($datetime ));
echo $unix_time;

My result looks like this:

20120725023508

Any idea what I'm doing wrong?

like image 712
user1216398 Avatar asked Oct 09 '12 15:10

user1216398


People also ask

What is Unix timestamp in PHP?

Simply put, the Unix timestamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the Unix timestamp is merely the number of seconds between a particular date and the Unix Epoch.

How do I get timestamp from DateTime?

You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.

How do I display a Unix timestamp?

To find the unix current timestamp use the %s option in the date command. The %s option calculates unix timestamp by finding the number of seconds between the current date and unix epoch.


1 Answers

strtotime Returns a timestamp on success, FALSE otherwise.

 echo strtotime('2012-07-25 14:35:08' );

Output

1343219708
like image 77
Baba Avatar answered Sep 18 '22 21:09

Baba