Log in

View Full Version : Logic Question


aubinhick990
10-03-2006, 22:28
This is a PHP website....

Okay, I have these variables...
$itemE_year
$itemE_month
$itemE_day
$itemE_hour
$itemE_minute
$itemE_ampm

$year = date("Y");
$month = date("m");
$day = date("d");

$hour = date("h");
$minute = date("i");
$ampm = date("A");
Basically they're self-explanatory....the $itemE_x is just a number being assigned via the contents of a text file.

Now, my question is what logic would I need if I wanted to check if the Item's year/month/day and hour/minute has passed my server's year/month/day, hour/minute.

This is the logic I've come up with but it doesn't work quite right...

if( (($year >= $itemE_year)) )
{

if($month == $itemE_month)
{
if($day >= $itemE_day)
{
$DISPBOX = "false";
echo "Time has passed!";
}
}

if( (($hour >= $itemE_hour) and ($minute >= $itemE_minute)) and ($ampm == $itemE_ampm) )
{
Whatever else I'm doing....
}

Any suggestions? Thanks a ton!

evulish
11-03-2006, 11:06
Is there any reason you can't store time() instead? If you need the d/m/y out of it later, you could use date() then.

Otherwise, the simplest way would be to use:


if (strtotime("$itemE_month/$itemE_day/$itemE_year $itemE_hour:$itemE_minute $itemE_ampm") > time()) { echo "yup"; }


It's more intensive than checking the logic but it's far simpler.



if ($item_year > $year) { echo "yes"; }
if ($item_year == $year) {
if ($item_month > $month) { echo "yes"; }
elseif ($item_month == $month) {
if ($item_day > $day) { echo "yes"; }
elseif ($item_day == $day) {
if ($item_hour > $hour) { echo "yes"; }
elseif ($item_hour == $hour) {
if ($item_minute > $minute) { echo "yes"; }
}
}
}
}



Another way would be:



$old = array(
$item_year,
$item_month,
$item_day,
$item_hour,
$item_minute
);

$new = array(
$year,
$month,
$day,
$hour,
$minute
);

function compare_dates($old, $new) {

for ($i=0;$i<4;$i++){
if ($old[$i] < $new[$i]) { return "no"; }
if ($old[$i] > $new[$i]) { return "yes"; }
}

return "equal";
}

echo compare_dates($old, $new);

aubinhick990
11-03-2006, 16:45
Wow thanks a lot. I can't do it right now but I'll work on that ASAP and get back to you about how it goes. As you can tell, I'm pretty novice when it comes to PHP but am still learning.