Click to See Complete Forum and Search --> : problem with dates


BuTcHoK
10-01-2003, 04:00 AM
i have a problem, comparing dates

$today=getdate();
$thismonth=$today["mon"];
$thisyear=$today["year"];
$thisdate="$thismonth/01/ $thisyear";

$tmonth=substr($date_prn,5,2);
$tdate=substr($date_prn,8,2);
$tyear=substr($date_prn,0,4);
$date="$tmonth/$tdate/$tyear";

$date=10/28/2002;10/01/2003
$thisdate=10/01/2003

if ($date < $thisdate) $date;

problem is...it returns the 2 dates!!! it should return only 1
which is 10/01/2003

could anyone help me with this???

pyro
10-01-2003, 07:25 AM
I'd be surprised if it would return anything, as you've got syntactical errors in it. Also, if you explain what you need to do, I'd be willing to bet there would be a better way of doing it...

BuTcHoK
10-01-2003, 07:31 PM
hello again mr. pyro...

i want to save data in the database dated before this month, e.g. october 1, 2003, so i have to get all data dated until sep 30, 2003... i also have to get all transactions for this month for printing purposes..


// i use this to get this month;
$today=getdate();
$thismonth=$today["mon"];
$thisyear=$today["year"];
$thisdate="$thismonth/01/ $thisyear";

// date_prn is extracted from database
$tmonth=substr($date_prn,5,2);
$tdate=substr($date_prn,8,2);
$tyear=substr($date_prn,0,4);
$date="$tmonth/$tdate/$tyear";

// and this is where i compare the two to save the data to database
if ($date < $thisdate)

// and this is to get the current month
($date[$i] >= $thisdate )

it "seems" right, but it only reads the month and not the entire date

... hope this can give you a clearer idea of what i mean...


but it only compares the month, not the enire date coz it still

pyro
10-01-2003, 09:04 PM
If you show me the format that the date is stored in the database, I'll try to show you a good way to compair them...

BuTcHoK
10-01-2003, 09:52 PM
// this format is extracted from database
2003-10-01 00:00:00

// but i can store dates to database using this format
10/01/2003

i dont know which you really mean, so im giving you the two formats

pyro
10-01-2003, 10:34 PM
Ok, I used the second of the two formats. Here's how I'd do it:

<?PHP
$dbdate = "10/01/2003"; #time from the database
$curtime = time(); #current time in a unix timestamp

list($month, $day, $year) = explode("/", $dbdate);
$dbtime = mktime(0,0,0,$month,$day,$year); #set the time in the database to be a unix timestamp

if ($dbtime < $curtime) { # if database time is less than current time
echo "Time in database is less (in the past) than current time";
}
else {
echo "Time in database is more (in the future) than current time";
}
?>Quite a bit smaller and cleaner than what you were trying... :)

BuTcHoK
10-04-2003, 12:55 AM
thank you so0o much, you're right, it's really simpler and cleaner!!! god bless!:)

pyro
10-05-2003, 05:45 PM
You are very welcome... I was happy to help. :)