-2

I want to get the difference between $_POST time and the time generated within the page after submit.

As I want to do an if statement which would be if the difference between the two times is less than 5seconds then ... else ...

Is using php date("H:i:s); the correct function?

Generate time

<?php date = date("H:i:s"); ?>

Echo time in hidden form field

<input name="tstmp" type="hidden" value="<?php echo $date; ?>"/>

Then in the page that the user get's directed to after form submit.

//could the problem be this is a string within the $_POST
$timestart = $_POST['tstmp'];

$timeend = date("H:i:s");

//can this only be used with timestamp?
$interval = $timestart->diff($timeend);
con322
  • 1,119
  • 7
  • 16
  • 29
  • `//can this only be used with timestamp?` No... BUT, since `diff()` is a method of the `DateTime` class it can only be called from objects from this class. What you have as a result of the `date()` function is a string!!! – Havelock Feb 09 '15 at 14:30
  • Why not just put a unix timestamp value into the form in the first place? (If you would just use `H:i:s`, I could send that same form again this time tomorrow or next week, which is probably not what you want …) – CBroe Feb 09 '15 at 14:44

2 Answers2

0

Take your time stamps and use strtotime() to convert them to unix time stamp and subtract them from eachother.

http://php.net/manual/en/function.strtotime.php

$timestart = $_POST['tstmp'];
$timeend =  date("H:i:s");

$timestart = strtotime($timestart);
$timeend   = strtotime($timeend);

if(($timeend - $timestart) > 5){
    echo 'TIME IS GREATER THAN 5 SECONDS';
}
dsadnick
  • 624
  • 1
  • 7
  • 25
-2

Convert your time stamp that you take in to a datetime object

$timeStart = new DateTime($timeStart);
$timeEnd = new DateTime($timeEnd);

Edited: fixed early morning coding brain didn't function right.

Asheliahut
  • 901
  • 6
  • 11
  • And how is this going to help? `$timeStart` is still a [string](http://php.net/manual/en/function.date.php#refsect1-function.date-returnvalues)... – Havelock Feb 09 '15 at 14:32