How to Convert Date to Timestamp in PHP

php convert a date to a timestamp

Always use four-digit years, as using two-digit years leads to endless headaches. 13 is not treated as a year; instead, 01-12-13 and 28-11-13 are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".

If you use 2013 instead, the issues disappear:

$today = date("d-m-Y");  // 01-12-2013
$start_date = "28-11-2013";

$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);

echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";

Output:

01-12-2013 > 28-11-2013
1385884800 > 1385625600

how to convert date and time to timestamp in php?

it gives error because mktime function require all values of numbers only and this function gives only date . if you try like

$h = 18;
$i = 11;
$s = 00;
$m = 07;
$d =23;
$y = 2009;
echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y));

then it will work.

so your complete code will be

date_default_timezone_set('UTC');

$d = str_replace('/', ',', '07/23/2009');
$t = str_replace(':', ',', '18:11');
$date = $t.',0,'.$d;
$fulldate = explode(',',$date);
echo '<br>';
$h = $fulldate[0];
$i = $fulldate[1];
$s = $fulldate[2];
$m = $fulldate[3];
$d =$fulldate[4];
$y = $fulldate[5];

echo date("h-i-s-M-d-Y",mktime($h,$i,$s,$m,$d,$y)) . "<br>";

//if you want timestamp
then use

echo strtotime("07/23/2009 18:11");

Thanks

How to convert date to timestamp in PHP inside array?

Loop your array by reference, and modify the column in your loop.

public function getAllData()
{
$data = $this->model_app->getRincian();
foreach ($data as $key => &$value) {
$timestamp = DateTime::createFromFormat("j-M-y", $value['TANGGAL_PENARIKAN'])->getTimestamp();
$value['TANGGAL_PENARIKAN'] = $timestamp;
}
return $data;
}

Convert date time into universal timestamp in PHP for calendars

Try this:

$date = '2009-09-12 15:00';
$formatted_date = gmdate('Ydm\THis\Z', strtotime($date));

Converting PHP Date to timestamp

There is two ways which I know.
use this :

$timenow = date('Y-m-d H:i:s');
$timestamp = strtotime($timenow);
echo 'time : ' . $timenow . '<br />';
echo 'MY test : ' . $timestamp;

My Output executed just now :

time : 2013-09-08 15:31:59
MY test : 1378647119

OR this:

$timestamp = time();

Convert date to integer (timestamp)

You can use one of this two ways to get the timestamp:

METHOD #1: Structured style.

// Use the strtotime() function
$timestamp = strtotime($Date1);

METHOD #2: Object-oriented style.

// DateTime class.
$date = new DateTime($Date1);
$timestamp = $date->getTimestamp();

NOTE ABOUT PERFORMANCE:

The structured style ( strtotime() ) is faster than the Object-oriented style ( DateTime ).



You can see an interesting benchmark of this two ways to get the timestamp here:

http://en.code-bude.net/2013/12/19/benchmark-strtotime-vs-datetime-vs-gettimestamp-in-php/

Convert one date format into another in PHP

The second parameter to date() needs to be a proper timestamp (seconds since January 1, 1970). You are passing a string, which date() can't recognize.

You can use strtotime() to convert a date string into a timestamp. However, even strtotime() doesn't recognize the y-m-d-h-i-s format.

PHP 5.3 and up

Use DateTime::createFromFormat. It allows you to specify an exact mask - using the date() syntax - to parse incoming string dates with.

PHP 5.2 and lower

You will have to parse the elements (year, month, day, hour, minute, second) manually using substr() and hand the results to mktime() that will build you a timestamp.

But that's a lot of work! I recommend using a different format that strftime() can understand. strftime() understands any date input short of the next time joe will slip on the ice. for example, this works:

$old_date = date('l, F d y h:i:s');              // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

Convert date string with timezone to timestamp

I fugured out !

uses the default time zone unless a time zone is specified in that
parameter
http://php.net/manual/en/function.strtotime.php

That's why the result is the same setting or not the timezone :

date_default_timezone_set('Europe/Paris');
print strtotime('2015-01-09T20:46:00+0200');
print "\n";
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');

print "\n\n";

date_default_timezone_set('UTC');
print strtotime('2015-01-09T20:46:00+0100');
print "\n";
print strtotime('2015-01-09T20:46:00');

output:

1420829160
1420832760
1420832760

1420832760
1420836360

Demo: https://eval.in/241781

Thanks for the help!



Related Topics



Leave a reply



Submit