Jump to content

[SOLVED] Timestamp Troubles


947740

Recommended Posts

I have a php feature that pulls a date out of a database and compares it to today's date minus 7 days and plus 7 days, for a total range of 14-15 days.  The timestamps seem to have minutes and seconds, because they are not "breaking even on the days".  The list of timestamps created for today are:

 

<option value='1224655200' >10/22/2008

<option value='1224741600' >10/23/2008

<option value='1224828000' >10/24/2008

<option value='1224914400' >10/25/2008

<option value='1225000800' >10/26/2008

<option value='1225087200' >10/27/2008

<option value='1225173600' >10/28/2008

<option value='1225260000' >10/29/2008

<option value='1225346400' >10/30/2008

<option value='1225432800' >10/31/2008

<option value='1225519200' >11/1/2008

<option value='1225605600' >11/2/2008

<option value='1225692000' >11/3/2008

<option value='1225778400' >11/4/2008

<option value='1225864800' >11/5/2008

<option value='1225951200' >11/6/2008

<option value='1226037600' >11/7/2008

<option value='1226124000' >11/8/2008

<option value='1226210400' >11/9/2008

<option value='1226296800' >11/10/2008

<option value='1226383200' >11/11/2008

<option value='1226469600' >11/12/2008

<option value='1226556000' >11/13/2008

<option value='1226642400' >11/14/2008

<option value='1226728800' >11/15/2008

<option value='1226815200' >11/16/2008

<option value='1226901600' >11/17/2008

<option value='1226988000' >11/18/2008

<option value='1227074400' >11/19/2008

 

For some reason, the timestamps are not working.  Some are 3600 off the correct timestamp.

 

$data['date'] is the date in timestamp format.  When I make the dropdown lists however, the only date that matches up with the database is the one for Nov.5 (This is the only value in November, the other ones are toward the end of October.)

 

while($data = mysqli_fetch_assoc($result)) {

$date = $data['date'];
$web = date("n/j/Y",$date);

// 2419200 is 28 days in seconds
for($new_time = $start_time;$new_time <= $start_time + 1209600;$new_time += 86400) {
$new_time = strtotime(date("n/j/Y",$new_time));
	if($date == $new_time) {
	$selected = "selected";

	}
	else {
	$selected = "";
	}

$times .= "<option value='$new_time' $selected>".date("n/j/Y",$new_time)."\n";
	if($data['date'] == "") {
	$data['date'] = strtotime("now");
	}
}
$stuff = stripslashes($data['announcement']);
echo "Date: <select name='time[".$data['ID']."]'>$times</select> Delete: <input type='checkbox' name='delete[".$data['ID']."]' value='delete' /><textarea 

name='".$data['ID']."'  rows='4' cols='1' class='textarea'>\n".$stuff."\n</textarea>\n<br />";
$times = "";
}

 

If any clarification is needed, just let me know what is not clear.

Link to comment
https://forums.phpfreaks.com/topic/131494-solved-timestamp-troubles/
Share on other sites

Just had a quick glance but first thing I noticed was $new_time is being used as a loop counter in the for() loop and inside the same loop you're assigning it again with strtotime() = best use a different variable for strtotime().

 

Next, please explain $start_time please...

Those are Unix timestamps and about the only use they are good for is doing greater/less-than comparisons. They must be converted to be used for anything else and they are subject to DST changes to convert them for display in your current time zone. The 1hour/3600 second error is probably because your DST/timezone database that php is using is not up to date.

 

You should use a DATE or DATETIME data type to store dates in a database. 2008-11-05 stored as a DATE data type will always be 2008-11-05, no matter what the server's clock says, what timezone php or mysql has be set to use, or how up to date the DST/timezone database is that php uses to convert Unix timestamps (and I am not even sure where mysql keeps its' DST/timezone information if you were using mysql to do the conversion instead of php.)

 

Also using a DATE or DATETIME will allow you to directly query for the information you want (untested but should work) -

 

SELECT your_columns FROM your_table WHERE your_date_column BETWEEN CURDATE() - INTERVAL 7 DAY AND CURDATE() + INTERVAL 7 DAY

Had a think about creating the list of options and came up with this...

 

First make an array using today's date:

$arrDT=explode(':',date('j:n:Y',time()));

 

Next, make an integer number of today's date:

$intDT=mktime(0,0,0,$arrDT[1],$arrDT[0],$arrDT[2]);

 

Calculate the number of seconds for one day:

60*60*24=86400

 

Calculate a week:

86400*7=604800

 

Next, use your for() loop to make the options:

$htmOptions='';
$intStartTime=$intDT-604800;
for ($i=1;$i<16;$i++) {
  $htmOptions.='<option value="'.$intDT.'"'.($intDT==$date ? ' selected="selected"' : '').'>'.date('n/j/Y',$intDT).'</option>';
  $intDT+=86400;
}

 

Not checked - hope that gives the idea. Now it's up to whether the value in $date frm your database is the correct value.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.