Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert date and time into unix timestamp in php?

echo $_POST['time']."<br/>";
echo $_POST['day']."<br/>";
echo $_POST['year']."<br/>";
echo $_POST['month']."<br/>";

I have value store like this now I want to create a timestamp from these value. How to do that in PHP? Thanks in advance

like image 402
Abhishek Avatar asked Apr 22 '10 12:04

Abhishek


People also ask

What is Unix timestamp in PHP?

Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Note: Unix timestamps do not contain any information with regards to any local timezone.

Which PHP function converts an English text DateTime into a Unix timestamp?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Which is a valid Strtotime () function in PHP?

The strtotime() function is a built-in function in PHP which is used to convert an English textual date-time description to a UNIX timestamp. The function accepts a string parameter in English which represents the description of date-time. For e.g., “now” refers to the current date in English date-time description.


2 Answers

You can use mktime(). Depending on the format of $_POST['time'], you split it into hour/min/sec and then use

$timestamp = mktime($hour, $min, $sec, $month, $day, $year)
like image 161
Matteo Riva Avatar answered Nov 14 '22 07:11

Matteo Riva


echo mktime(0,0,0,$_POST['month'],$_POST['day'],$_POST['year']);

I don't know in what format your time is, so, you probably need to explode() it and then put the values into the three first parameters of mktime() like:

$_POST['time'] = '8:56';
$time = explode(':',$_POST['time']);
echo mktime($time[0],$time[1],0,$_POST['month'],$_POST['day'],$_POST['year']);
like image 29
Tower Avatar answered Nov 14 '22 07:11

Tower