I post a date input $_POST['date']
with format 2013/11/22
and time $_POST['time']
with format 10:10 AM
. Now I need to put both inputs in the mktime
function like this:
mktime(10, 10, 0, 11, 22, 2013);
How do I create this?
I post a date input $_POST['date']
with format 2013/11/22
and time $_POST['time']
with format 10:10 AM
. Now I need to put both inputs in the mktime
function like this:
mktime(10, 10, 0, 11, 22, 2013);
How do I create this?
You can use DateTime::createFromFormat
to create a DateTime
object from an arbitrary format. Then you can use that object to get the timestamp (or format the date in another way):
// $_POST['date'] = '2013/11/22';
// $_POST['time'] = '10:10 AM';
$datetime = $_POST['date'] . ' ' . $_POST['time'];
$datetime = DateTime::createFromFormat('Y/m/d h:i A', $datetime);
if ($datetime) {
$timestamp = $datetime->getTimestamp();
} else {
echo 'Invalid date or time.';
}
The format in my solution (Y/m/d h:i A
) expects leading zeros for all parts of the input (e.g., 2013-01-01 01:01 AM
). If the input doesn't use leading zeros, you'll have to change the input format. You can see all supported format characters in the documentation.
You do not need to use mktime to it. Yeah entries are strings, then it is more consistent to use the strtotime function.
//$_POST['date'] = '2013/11/22';
//$_POST['time'] = '10:10 AM';
$dateTime = "{$_POST['date']}{$_POST['time']}";
$timeStamp = strtotime($dateTime);
if($timeStamp===false):
throw new \Exception("The date or time format is not valid!");
endif;