Jump to content

date function


Woodburn2006

Recommended Posts

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:

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

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

[code]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>";}
 
  }[/code]

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
Link to comment
Share on other sites

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:

[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";

?>[/code]

and i got this result:

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

any ideas why they are so different?
Link to comment
Share on other sites

Please show us how you are storing the data in the database. Use this code:
[code]<?php
while ($row_present = mysql_fetch_assoc($rs_present))
    echo '<pre>' . print_r($row_present,true) . '</pre>';
?>[/code]
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
Link to comment
Share on other sites

thanks,

ive used

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

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

and then used this to display it:

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

}[/code]


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'
Link to comment
Share on other sites

Wood:

[code]<?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>";

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.