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
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
Use a DateTime object. The parsing is done for you and you can control for TimeZone differences so that daylight savings time doesn't cause issues.
$datetimestring="{$_POST['year']}-{$_POST['month']}-{$_POST['day']} {$_POST['time]}";
$dt= date_create($datetimestring);//optional 2nd argument is TimeZone, so DST won't bite you
echo $dt->format('U');
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']);
Another option is strtotime:
$time = "{$_POST['time']} {$_POST['month']}/{$_POST['day']}/{$_POST['year']}";
$timestamp = strtotime($time);
If $_POST['time']
is already in hours and minutes (HH:MM
format), then this saves you the step of exploding and parsing it. strtotime
can parse a dizzying number of strings into valid times.