Forum Moderators: coopster

Message Too Old, No Replies

Comparing entered date and time to PHP date and time - How?

         

dwest

2:39 am on Feb 20, 2008 (gmt 0)

10+ Year Member



Hi,
I've got a script which takes a date value I enter like so: 2008/02/19

The script compares that value to the current date - date("Ymd") - and does some stuff based on the comparison.

I need to enhance the script by making it compare the date AND time
That is what I need some guidance with.

I would like to enter my value as: 2008/02/19 18:05:00 (note the space between the date and the time).

Then have the script compare that to the current date and time.

How can I do that?

Thanks!

jatar_k

3:16 am on Feb 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about storing a timestamp instead, makes all kinds of math really simple and a quick format function is easy for output

dwest

3:28 am on Feb 20, 2008 (gmt 0)

10+ Year Member



Hmmm...
I have to enter a value in the form that is human friendly. Thus 2008/02/19 18:20:00.

So given that, in the script how do I get from my entered value (which the script captures and stores) to a time stamp so I can compare the entered time stamp to the current unix time stamp?

vincevincevince

3:30 am on Feb 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about using strtotime() on it?

$secondsbetween = time()-strtotime($_GET['time']);

jatar_k

3:32 am on Feb 20, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



do they enter it as a string or from drop downs?

drop downs are common, you can plug the values from the drop downs into mktime() [php.net]

dwest

3:39 am on Feb 20, 2008 (gmt 0)

10+ Year Member



It gets entered manually as 2008/02/19 18:00:00.
I can't change it to dropdowns unfortunately.

dwest

3:42 am on Feb 20, 2008 (gmt 0)

10+ Year Member



vincevincevince...

So in your snippet:
$secondsbetween = time()-strtotime($_GET['time']);

Where does my entered value get plugged in?
Basically I'm storing that entered value in a variable, lets call it myvalue.

Where does myvalue plug into your snippet?

dwest

3:48 am on Feb 20, 2008 (gmt 0)

10+ Year Member



Here is what I've got after vincevincevince's suggestion:
Will this work?

<?php
//to check against expiration date;
$currenttime = time();
$expirationtime = get_post_custom_values('expiration');//ENTERED AS 2008/02/19 18:00:00
if (is_null($expirationtime)) {
$expirestring = '30005050'; //MAKE UN-EXPIRING POSTS ALWAYS SHOW UP;
} else {
$expirestring = strtotime($expirationtime);
}

if ( $expirestring > $currenttime ) { ?>
//HTML STUFF HERE
<?php
}
?>

vincevincevince

3:52 am on Feb 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_GET['time'] = your value. If your form uses GET and a field named 'time' then leave it as it is. If your form uses POST and a field named 'time' then it would be $_POST['time']

dwest

3:56 am on Feb 20, 2008 (gmt 0)

10+ Year Member



vincevincevince,
Please see my reply just before your last one...

dwest

5:09 am on Feb 20, 2008 (gmt 0)

10+ Year Member



Resolved this - thanks vincevincevince.