Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a date YYYY-MM-DD to epoch in PHP [duplicate]

Tags:

date

php

time

epoch

As per title: how to convert a string date (YYYY-MM-DD) to epoch (seconds since 01-01-1970) in PHP

like image 783
Gianluca Ghettini Avatar asked Mar 13 '13 08:03

Gianluca Ghettini


People also ask

How to convert date into epoch in PHP?

The PHP 5 DateTime class is nicer to use: // object oriented $date = new DateTime('01/15/2010'); // format: MM/DD/YYYY echo $date->format('U'); // or procedural $date = date_create('01/15/2010'); echo date_format($date, 'U');

How do I convert date to epoch?

Convert from human-readable date to epoch long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

How can get date in dd-mm-yyyy format in PHP?

Answer: Use the strtotime() Function You can first use the PHP strtotime() function to convert any textual datetime into Unix timestamp, then simply use the PHP date() function to convert this timestamp into desired date format. The following example will convert a date from yyyy-mm-dd format to dd-mm-yyyy.

How to use strtotime in PHP?

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.


3 Answers

Perhaps this answers your question

http://www.epochconverter.com/programming/functions-php.php

Here is the content of the link:

There are many options:

  1. Using 'strtotime':

strtotime parses most English language date texts to epoch/Unix Time.

echo strtotime("15 November 2012");
// ... or ...
echo strtotime("2012/11/15");
// ... or ...
echo strtotime("+10 days"); // 10 days from now

It's important to check if the conversion was successful:

// PHP 5.1.0 or higher, earlier versions check: strtotime($string)) === -1
if ((strtotime("this is no date")) === false) {
   echo 'failed';
 }

2. Using the DateTime class:

The PHP 5 DateTime class is nicer to use:

// object oriented
$date = new DateTime('01/15/2010'); // format: MM/DD/YYYY
echo $date->format('U'); 

// or procedural
$date = date_create('01/15/2010'); 
echo date_format($date, 'U');

The date format 'U' converts the date to a UNIX timestamp.

  1. Using 'mktime':

This version is more of a hassle but works on any PHP version.

// PHP 5.1+ 
date_default_timezone_set('UTC');  // optional 
mktime ( $hour, $minute, $second, $month, $day, $year );

// before PHP 5.1
mktime ( $hour, $minute, $second, $month, $day, $year, $is_dst );
// $is_dst : 1 = daylight savings time (DST), 0 = no DST ,  -1 (default) = auto

// example: generate epoch for Jan 1, 2000 (all PHP versions)
echo mktime(0, 0, 0, 1, 1, 2000); 
like image 118
Nick Audenaerde Avatar answered Oct 23 '22 20:10

Nick Audenaerde


Try this :

$date  = '2013-03-13';

$dt   = new DateTime($date);
echo $dt->getTimestamp();

Ref: http://www.php.net/manual/en/datetime.gettimestamp.php

like image 37
Prasanth Bendra Avatar answered Oct 23 '22 22:10

Prasanth Bendra


Use the strtotime() function:

strtotime('2013-03-13');
like image 3
Michal M Avatar answered Oct 23 '22 21:10

Michal M