22

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

j0k
  • 22,600
  • 28
  • 79
  • 90
Gianluca Ghettini
  • 11,129
  • 19
  • 93
  • 159
  • 3
    Did you try to search on SO before asking your question? There is a lot of questions related to this topic. Here is on: [mm/dd/yyyy format to epoch with PHP](http://stackoverflow.com/questions/359782/mm-dd-yyyy-format-to-epoch-with-php) – j0k Mar 13 '13 at 08:53
  • the worse is all the people using unnecessary acronyms and making unnecessary comments: what's SO? maybe did you mean SE (Search Engine), didn't u? – Gianluca Ghettini Mar 13 '13 at 09:09
  • SO means StackOverflow, sorry. SE could mean StackExchange network. Meta means meta.stackoverflow. – j0k Mar 13 '13 at 09:49

4 Answers4

38

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); 
altsyset
  • 339
  • 2
  • 20
Nick Audenaerde
  • 967
  • 2
  • 14
  • 33
  • 9
    Lone link is considered a poor answer (see [faq#deletion]) since it is meaningless by itself and **target resource is not guaranteed to be alive in the future**. [It would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – j0k Mar 13 '13 at 08:52
  • 1
    Sorry j0k won't happen again :) – Nick Audenaerde Mar 13 '13 at 08:55
13

Try this :

$date  = '2013-03-13';

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

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

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • 1
    Interesting tidbit about this answer. It works only for dates past the UNIX epoch. Not that many people need earlier times, but it will not output a negative. – The Thirsty Ape Jan 07 '14 at 19:07
4

Use the strtotime() function:

strtotime('2013-03-13');
Michal M
  • 9,322
  • 8
  • 47
  • 63
3

use strtotime() it provides you Unix time stamp starting from 01-01-1970

Rohit Choudhary
  • 2,253
  • 1
  • 23
  • 34