Click to See Complete Forum and Search --> : PHP Date Problems


mrBen
11-28-2002, 10:34 AM
OK - this has me stumped.

Below is a script that adds dates into a booking table for the holiday cottage website that I administer. I can't see any problems with it, and yet for some unknown reason when I use it, it mysteriously changes from being Saturday dates to Friday days on the 1st November 2002. Without fail.

Hayelp.



<?
// add_dates2.php

// This file adds dates to the database up to the given date (but not including)
// Open the database connection
$connection = @mysql_connect ("localhost", "username", "se1<r3t" ) or die ("Unable to access bookings database" );
if ($connection) {
$msg = "Connected to Grinills Bookings Database" ;
}

$db = @mysql_select_db("mydatabase", $connection) or die ("Unable to access table");

// Grab the date from the form on the previous pag

$end_time=mktime(0,0,0, $month,$day,$year);


// Grab all the dates in numerical order

$sql = "select start from bookings order by start desc";

$result = @mysql_query($sql,$connection) or die ("Unable to access data");

$temparray = mysql_fetch_array($result);

// If there are dates in the table, then start from the last date

if ($temparray['start'] == 0) {
$day = "03";
$month = "08";
$year = "02";
}
else {
$year = substr ($temparray['start'], 0, 4);
$month = substr ($temparray['start'], 5, 2);
$day = substr ($temparray['start'], 8, 2);
}

// Double check the date is a Saturday

$error = (date("l", mktime(0,0,0,$month,$day,$year)) == "Saturday") or die("Internal date error");


// Start from the next weekend
$start_time=mktime(0,0,0,$month,$day,$year) + 604800;

while ($start_time < $end_time) {
$error = date("d-m-Y", $start_time);
$sql = "insert into bookings (start) values ('";
$sql .= date("Y-m-d", $start_time);
$sql .= "')";
$result = @mysql_query($sql,$connection) or die("Error adding date $error");
$start_time = $start_time + 604800;
}

?>

<html>

<head>

<title>Grinills -> Bookings -> Add Dates -> Added up to <? echo date ("l j M Y", $end_time) ?></title>

</head>

<body>

<img src="grinillsadminbanner.png"></img>

<center><h1>Adding dates....</h1></center>

Dates from <? echo date("l j M Y", mktime(0,0,0,$month,$day,$year)) ?> up until <? echo date("l j M Y", $end_time) ?> added.

<br><br>

<a href="booking_menu.php">Click here to return to Booking Menu</a>



</body>

</html>

mrBen
11-28-2002, 12:39 PM
*bump*

Rüpel
11-28-2002, 05:09 PM
i would like to help, i think i understand the script, but i don't understand your problem. which date is changed when?

mrBen
11-29-2002, 04:07 AM
Originally posted by Rüpel
i would like to help, i think i understand the script, but i don't understand your problem. which date is changed when?

The dates are all supposed to be Saturdays, and yet for some reason from 1st November, the dates started being Fridays.

I asked the question at my local LUG last night, and somebody suggested something which turned out to be the answer:

All the dates are calculated using mktime and date, and because I wasn't interested in time, I just used mktime(0,0,0,$month,$day,$year) which is midnight. But when it changed from daylight saving (BST here) to normal time (GMT) it went back a day. I checked this on the dates, and realised that for the period between March 31 and Oct 31 the following year the dates were Saturdays again, so I'll just change the script to use midday instead.