my current code:
<?php
$timestamp = DateTime::createFromFormat("d.m.Y H:i", $_POST["datetime"]);
echo $timestamp; // works!
?>
Now I've a form with two fields: date and time, separated.
<?php
$timestamp = DateTime::createFromFormat("d.m.Y", $_POST["date"]);
echo $timestamp; // works!
$timestamp = DateTime::createFromFormat("H:i", $_POST["time"]);
echo $timestamp; // doesn't work (error below)!
// Edit:
/* Fatal error: Call to a member function getTimestamp() on a non-object in */
?>
I would like to avoid using explode() with mktime(). That feels dirty and I think there could be another, clean way.
How would you create the timestamp from format?
Thanks in advance!
If you want to get the date and time from $_POST["date"]
and $_POST["time"]
Use following
DateTime::createFromFormat("d.m.Y H:i", $_POST["date"]." ".$_POST["time"]);
If you want to kow the number of seconds from midnight for that particular time you can however use this,
DateTime::createFromFormat("Ymd H:i", date("Ymd")." ".$_POST["time"]);
You can't. The timestamp requires a date in order to be calculated, and there isn't one available to use.
Use Date time's settime method
$timestamp = \DateTime::createFromFormat("d.m.Y", '10.10.2012');
$timestamp->setTime(17,12);
var_dump($timestamp);
Gives
object(DateTime)#1142 (3) { ["date"]=> string(19) "2012-10-10 17:12:00" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" }