How to convert YYYY-MM-DD date to era in PHP

According to the title: how to convert the date of a string (YYYY-MM-DD) to an era (seconds from 01-01-1970) in PHP

+5
source share
4 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:

  • Using 'strtotime':

strtotime parses most English texts for eras / Unix Time.

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

It is important to verify the success of the conversion:

// 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 more convenient 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 complex, 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); 
+12

:

$date  = '2013-03-13';

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

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

+9

use strtotime()it provides you with a Unix timestamp from 01-01-1970

+2
source

Use the strtotime () function :

strtotime('2013-03-13');
+2
source

All Articles