...

View Full Version : input form date check help



PRodgers4284
02-21-2008, 03:17 PM
Im trying to check a date is equal to after the current date but i cant same to get it to work, it just bringing up an error, can anyone help?

My code is:


if $date >= NOW or if ($date >= date();
{

$error_stat = 1;

//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}

abduraooft
02-21-2008, 03:30 PM
I'm not sure whether it's possible to compare date in PHP like that, but you what was the error?


if $date >= NOW or if ($date >= date();
{
if should be of the form

if(condition1 || condition2) in your case.

_Aerospace_Eng_
02-21-2008, 03:38 PM
What is $date and what do you think NOW is?

PRodgers4284
02-21-2008, 03:41 PM
I'm not sure whether it's possible to compare date in PHP like that, but you what was the error?

if should be of the form

if(condition1 || condition2) in your case.

Thanks for the reply, I have the following code, but its not working properly, just outputs invalid date, can you help here?


if ($date <= NOW || $date <= date);
{

$error_stat = 1;

//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}

PRodgers4284
02-21-2008, 03:45 PM
What is $date and what do you think NOW is?

$date is the variable name i have for the input field date, am i doing this completely wrong?

This is the full code for the date check


//Date check)

if (empty($date)) {
//Set the error_stat to 1, which means that an error has occurred
$error_stat = 1;

//Set the message to tell the user to enter a dob
$date_message = '*Please enter job closing date*';
}

//Check the format and explode into $parts
elseif (!ereg("^([0-9]{4})/([0-9]{2})/([0-9]{2})$",
$date, $parts)){
$error_stat = 1;

//Set the message to tell the user the date is invalid
$date_message = '*Invalid closing date, must be YYYY/MM/DD format*';
}

elseif (!checkdate($parts[2],$parts[1],$parts[3]))
{
$error_stat = 1;

//Set the message to tell the date is invalid for the month entered
$date_message = '*Invalid date, month must be between 1-12*';
}

if ($date <= NOW || $date <= date);
{

$error_stat = 1;

//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}

_Aerospace_Eng_
02-21-2008, 03:53 PM
I guess what I'm asking is what is the form of $date? What does the example input look like? Also again what do you think NOW is? It almost seems like you are trying to get NOW() from mysql yet you don't connect anywhere to mysql. And yes you are doing this wrong. We need more info though. Here is an example of how you can determine if the date is pass the current date or equal to it.

<?php
$date = mktime(0, 0, 0, 3, 1, 2008);
echo 'Date is : ' . $date .' milliseconds since January 1 1970 00:00:00 GMT.<br>';
echo 'The time since January 1 1970 00:00:00 GMT is ' . time() . ' in milliseconds.<br>';
if ($date >= time())
{
$error_stat = 1;
//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}
echo 'Date message is ' . $date_message . '<br>';
echo 'Error stat is ' . $error_stat;
?>
You use mktime() (http://us.php.net/manual/en/function.mktime.php) to create a unix timestamp.

PRodgers4284
02-21-2008, 04:02 PM
I guess what I'm asking is what is the form of $date? What does the example input look like? Also again what do you think NOW is? It almost seems like you are trying to get NOW() from mysql yet you don't connect anywhere to mysql.


The input is yyyy/mm/dd in the database and the type is set as "date" in the database. Im not sure what i was doin NOW() :o Sorry for the confusion. Appreciate your help on this.

_Aerospace_Eng_
02-21-2008, 04:10 PM
Try this

<?php
$date = '2008/02/21';
$datetime = strtotime($date);
$todaysdate = strtotime(date('Y\/m\/d'));
if ($datetime >= $todaysdate)
{
$error_stat = 1;
//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}
else
{
$error_stat = 0;
$date_message = '*Valid date*';
}
echo 'Error stat is: ' . $error_stat . '<br>';
echo 'Date message is: ' . $date_message;
?>
I was using time() instead of todays date. Changes should be correct.

PRodgers4284
02-21-2008, 04:31 PM
Try this

<?php
$date = '2008/02/21';
$datetime = strtotime($date);
$todaysdate = strtotime(date('Y\/m\/d'));
if ($datetime >= $todaysdate)
{
$error_stat = 1;
//Set the message to tell the user the date is invalid for the year entered
$date_message = '*Invalid date*';
}
else
{
$error_stat = 0;
$date_message = '*Valid date*';
}
echo 'Error stat is: ' . $error_stat . '<br>';
echo 'Date message is: ' . $date_message;
?>
I was using time() instead of todays date. Changes should be correct.



Thanks for that aerospace, that worked fine, appreciate your help



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum