As per title: how to convert a string date (YYYY-MM-DD) to epoch (seconds since 01-01-1970) 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');
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.
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.
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.
Perhaps this answers your question
http://www.epochconverter.com/programming/functions-php.php
Here is the content of the link:
There are many options:
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.
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 the strtotime() function:
strtotime('2013-03-13');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With