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

℡╲_俬逩灬. 提交于 2019-12-03 11:16:52
Nick Audenaerde

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); 

Try this :

$date  = '2013-03-13';

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

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

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

Use the strtotime() function:

strtotime('2013-03-13');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!