Jump to content


Photo

date function


  • Please log in to reply
16 replies to this topic

#1 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 10:14 PM

i am trying to get a date from a database and comparing it to todays date, then depending on the date i want to display it or not display it. but i find that when using the time() function to return todays date in seconds, it is out of sinc with the returned results from the database this is the code that i use to get todays date in seconds:

$today_seconds = time(); // todays date in seconds from jan 1, 1970

then this is the code that i use to sort out the dates that iwant once they have been returned from the database.

while ($row_present = mysql_fetch_array($rs_present)) 
   	 {
	   $startdate_present = $row_present[0]; 
	   $exhibition_present = $row_present[1]; 
	   $dateend_present = $row_present[2];
	   
	   $start_present = split("-",$startdate_present);
	   $end_present = split("-",$dateend_present);
	   $startdate_seconds_present = mktime(0,0,0,$start_present[0],$start_present[1],$start_present[2]); 
	   $enddate_seconds_present = mktime(0,0,0,$end_present[0],$end_present[1],$end_present[2]); 
	   if ($startdate_seconds_present < $today_seconds && $enddate_seconds_present > $today_seconds){echo "$startdate_present - $dateend_present: $exhibition_present<br>";}
	   
	   }

using this i always found that i would have odd outcomes. so i decided to change the date in the database to the same as todays date (31/7/2006) and see what the outcome in seconds was and this is what i got.

31/7/2006 from DB: 1215385200
31/7/2006 from system: 1154383906

it seems from this that one of the dates does not start from jan 1st 1970

any help would be very much appreciated.

thanks alot

#2 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 31 July 2006 - 10:45 PM

If you're storing dates as timestamps in your database, what method are you using? time()?


PHP Ninja

#3 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 10:58 PM

i cannot even get an output from that. i put in the code you said and then put in echo $dbdate; where i wanted it to go and nothing appeared

any other ideas?

#4 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 31 July 2006 - 11:05 PM

<?php

$currdate = time();
$convert = date("d/m/Y",$currdate);

echo "Date = $convert<br>";
echo "Timestamp = $currdate<br>";

?>

Try this little simple conversion. It should echo out the timestamp as today's date.
PHP Ninja

#5 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 11:22 PM

thanks, that proves that the date coming from the system is ok, but when i use the same date and convert it with mktime() function i get a different rsult.

i used this code:

<?php

$currdate = time();
$convert = date("d/m/Y",$currdate);

echo "Date = $convert<br>";
echo "Timestamp = $currdate<br>";

$todays_date = "01-08-2006";
	   
$start_present = split("-",$todays_date);
$start = mktime(0,0,0,$start_present[0],$start_present[1],$start_present[2]);
echo "After mktime convert = $start";

?>

and i got this result:

Date = 01/08/2006
Timestamp = 1154388039
After mktime convert = 1136678400

any ideas why they are so different?

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 31 July 2006 - 11:25 PM

Please show us how you are storing the data in the database. Use this code:
<?php
while ($row_present = mysql_fetch_assoc($rs_present)) 
    echo '<pre>' . print_r($row_present,true) . '</pre>';
?>
Post the first record or so that displays on your screen. From that we will have a better handle on how to help you.

Ken


#7 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 11:27 PM

mktime is months BEFORE day (the American date order).. so 01/08 is actually January 08
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#8 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 11:29 PM

Array
(
    [DATE_FORMAT(startdate, '%d-%m-%Y')] => 30-07-2006
    [exhibition] => Paul Newman
    [DATE_FORMAT(enddate, '%d-%m-%Y')] => 06-10-2028
)

that is the first

the date is stored in the database as: YYYY-MM-DD using the 'date' datatype

thanks

#9 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 11:39 PM

well if you already use the "date" datatype, then you can use the WHERE clause
SELECT * FROM table WHERE CURDATE() BETWEEN startdate AND enddate

this will be much more consistent.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#10 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 11:44 PM

ok thanks, im quite new so could you please show how i would use the curdate method instead please? i want to get the dates from the database and somehow get them into the same format as todays date so that i can display the ones i want

thanks

#11 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 11:46 PM

oh CURDATE() is just a function to return today's date in YYYY-MM-DD format.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#12 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 31 July 2006 - 11:48 PM

ok, is there anyway i could use that to compare to the dates that i get from the database?

#13 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 31 July 2006 - 11:52 PM

in a query
SELECT * FROM table WHERE CURDATE() >= startdate

or if you prefer to select it
$query = "SELECT CURDATE()";

Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#14 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 01 August 2006 - 12:03 AM

thanks,

ive used

$sql_new = "SELECT * FROM events WHERE CURDATE() >= startdate";

	$result = mysql_query($sql_new, $connection);
    if (mysql_error()) { print "Database Error: $sql " . mysql_error(); }

and then used this to display it:

while ($row_new = mysql_fetch_array($result)) {
	echo $row_new[0];

}


but i get the result '23', what would the variable be?

i have tried $row_new[0]; and $startdate but '$startdate' has nothing and $row_new[0] has the value of '23'

#15 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 01 August 2006 - 12:09 AM

extract($row_new)
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#16 Caesar

Caesar
  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 01 August 2006 - 12:12 AM

Wood:

<?php

$currdate = time();
$convert = date("d-m-Y",$currdate);

$date2 = '31-07-2006';
$convert2 = strtotime($date2);
$arr = explode('-',$date2);
$format = "".$arr[0]."-".$arr[1]."-".$arr[2]."";

echo "$convert<br>";
echo "$format<br>";

?>

PHP Ninja

#17 Woodburn2006

Woodburn2006
  • Members
  • PipPipPip
  • Advanced Member
  • 214 posts

Posted 01 August 2006 - 12:18 AM

cool thanks everyone for your help

saved my life
thanks again




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users