Forum Moderators: coopster

Message Too Old, No Replies

date function

         

nshack31

1:39 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



can someone tell me why rather than output todays date, the following is outputting 17th decemeber 2004?


$today = date("F-j-Y, g:i a", mktime(date("g"), date("i"), date("a"), date("F"), date("j"), date("Y")));

echo $today;

thanks!

gettopreacherman

2:37 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



It appears to me that your server time may be skewed...other than that, it appears okay.

nshack31

3:43 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



sorted it with..

$today = date("F-j-Y, g:i:a");
echo $today;

but what if i wish to compare this with dates I have stored in a variable? i am using..

$date_to_compar = date("F-j-Y, g:i:a", mktime($hour, $minute, $ampm, $month, $day, $year));

if($today > $date_to_compar)

but $date_to_campar will not work. What must I change that to?

thanks again

baze22

3:49 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



You are really comparing strings, not dates. If you want to compare dates, use the timestamps and then once compared, convert to strings

$today = time();
echo $today;

$date_to_compar = mktime($hour, $minute, $ampm, $month, $day, $year);

if($today > $date_to_compar) then convert...

disclaimer: my code above is untested off the top of my head and may contain errors, but should give you the basic idea. ;)

baze

nshack31

8:25 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



hmm that didn't work due to my ampm variable etc.. here is what I am doing..

the values are first obtained from a form


$day=$_REQUEST['day']; //read as integer ie 28
$month=$_REQUEST['month']; //read as name ie February
$year=$_REQUEST['year']; //read as full year ie 2005
$hour=$_REQUEST['hour']; //read as 1 - 12 ie 1
$minute=$_REQUEST['minute']; read as 1-60 ie 15
$ampm=$_REQUEST['ampm']; read as am/pm ie am

i then set todays date and obtain the date from the form..


$today = date("F-j-Y, g:i:a");
$date_to_compar = date("F-j-Y, g:i:a", mktime($hour, $minute, $ampm, $month, $day, $year));

I then wish to validate it..


if($today > $date_to_compar)
{
echo $date_to_compar;
echo $today;
die('Error, date has passed! <a href="javascript:history.go(-1)">back</a>');
}

I can do it for date no probs but i want it to check if the date and time has passed. Any ideas? thanks so far

baze22

9:01 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



This works:


$day='8';
$month='February';
$year='2005';
$hour= '1';
$minute='15';
$ampm='am';

$datestr = sprintf("%s %s, %s %s:%s%s",$month,$day,$year,$hour,$minute,$ampm);
$mytimestamp = strtotime($datestr);
$today = time();
echo "variable date: " . date("F-j-Y, g:i:a",$mytimestamp);
echo "<br>today date: " . date("F-j-Y, g:i:a",$today);
if ($today < $mytimestamp){

echo "<br>after today";
} else {

echo "<br>today is later";
}

Just remember the date() function returns strings not numbers or dates.

baze

nshack31

10:06 pm on Feb 17, 2005 (gmt 0)

10+ Year Member



strtotime was the answer! thanks :)