Jump to content

PHP Time Validation


ProcalX

Recommended Posts

I have a MYSQL database, within this database is a table that has three columns: day/month/year in this format: 10/10/2010. I am trying to validate a script so that a user cannot cancel a booking if it is 48 hours before the event, or the event has past.

 

This is what I have so far:

 

<?php
$bookingid = $_GET["id"];
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 days"); //todays date +2 days

if ($bookingid != "")
{
	//check both events
	$sql = "SELECT bookings.ID AS bid, events.* FROM bookings,events WHERE bookings.id = $bookingid AND bookings.eventID = events.ID";
	$result = mysql_query($sql);
	$row = mysql_fetch_array($result);
	$eventdate = date("Y-m-d","$row[year]-$row[month]-$row[day], int mktime ([ int $hour = 0 [, int $minute = 0 [, int $second = 0 [, int $month = $row[month] [, int $day = $row[day] [, int $year = $row[year] [, int $is_dst = -1 ]]]]]]] )");

	if ($eventdate < $date)
	{
			echo "Event Date: $eventdate <br /><br />";
			echo "Todays Date + 2 Days: $date<br /><br />";
			$sql = "SELECT * FROM bookings WHERE ID = $bookingid";
			$result = mysql_query($sql);
			$row = mysql_fetch_array($result);

			if (mysql_num_rows($result)==1)
			{
			//Update quantity of tickets for event
			$mysql = "UPDATE events SET availability = availability + $row[ntickets] WHERE ID = $row[eventID]";
			mysql_query($mysql);

			//Delete booking from bookings table
			$mysql = "DELETE FROM bookings WHERE ID = $bookingid";
			mysql_query($mysql);

			echo "You have succesfully cancelled booking id: $bookingid";
			}
	}
	else
	{
		echo "You cannot cancel a booking 48 hours before the event.";
	}
}
mysql_close($conn);
?>	

 

The script runs fine, however it does not validate properly, Event Date shows as: 1970-01-01 and Todays date + 2 days shows as: 172800. Unsure why event date is showing like this, I believe this is the earliest date that unix can handle?

Link to comment
https://forums.phpfreaks.com/topic/192116-php-time-validation/
Share on other sites

hai

in ur code

i dont got this query

$eventdate = date("Y-m-d","$row[year]-$row[month]-$row[day], int mktime ([ int $hour = 0 [, int $minute = 0 [, int $second = 0 [, int $month = $row[month] [, int $day = $row[day] [, int $year = $row[year] [, int $is_dst = -1 ]]]]]]] )");

 

thw $sql doesnot have any row month or day or year..

are u sure u wrote the correct one

Link to comment
https://forums.phpfreaks.com/topic/192116-php-time-validation/#findComment-1012493
Share on other sites

The $sql selects all from the BOOKINGS table where the ID matches that of the bookingID, this will select everything within the bookings table subject to these parameters, which includes the columns: day / month / year.

 

The idea here was to insert straight from the mysql_fetch_array the day / month / year straight into the PHP DATE function and store it to $eventdate variable so that the full date of the event is stored from the database into a variable within the script.

 

This may be wrong but I am unsure how to go about this? Can you help?

Link to comment
https://forums.phpfreaks.com/topic/192116-php-time-validation/#findComment-1012508
Share on other sites

Thanks for your help but I've sorted it;

 

Work out todays date + 2 days:

$twodays  = time()+60*60*24*2;

This stores the date 2 days ahead of the present day in seconds then we check this against the date stored in the database via the mysql_fetch_array:

$eventdate = strtotime("$row[year]-$row[month]-$row[day]");

This converts the day, month and year stored in the database into the same time format (in unix timestamp seconds)

 

Then check against each other using an If statement. I hope this helps anyone else!

 

Link to comment
https://forums.phpfreaks.com/topic/192116-php-time-validation/#findComment-1012515
Share on other sites

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.