Checking mysql date against php date

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
t_rob76
Forum Newbie
Posts: 2
Joined: Thu May 28, 2009 11:05 pm

Checking mysql date against php date

Post by t_rob76 »

I have a php script that inserts a set date into a table, it also inserts time every 6 minutes for x hours as well as an availability column. The first thing I do is check the database to make sure that the insert date has not already posted, if it has the else statement tells me that it has already been posted. Unfortunately I cannot get the date retrieved from mysql to equal the date in php

here is the code

Code: Select all

<?php
include "mysql.php";
$schddate = date('Y-m-d');
$open = '8:00';
$closed = '16:00';
$StartTime = strtotime($open);
$EndTime = strtotime($closed);
$availability = 4;
 
 
for($i = $StartTime; $i <= $EndTime; $i += 6 * 60) { //6 minutes. In this format it makes it easy to change the minutes
  $temp_array[] = $i;
 
} 
   [color=#BF0000] $query = "SELECT schdDate FROM schedule " .[/color]
            [color=#BF0000] "WHERE schdDate = " . $schddate . ";";[/color]
    [color=#BF0000]$result = mysql_query ($query)[/color]
       [color=#BF0000] or die(mysql_error());[/color]
    
            
[color=#BF0000]if (mysql_num_rows($result) != 0) {[/color]
    echo "The schedule for that date has already been posted";
        
    } else {
        foreach ($temp_array as $time1) {
        $time2 = date('H:i',$time1);
        
            $query = "INSERT INTO schedule (schdDate, schdTime, schdAvail) " .
                     "VALUES ('" . $schddate . "','" . $time2 . "','" . 
                             $availability . "');";
                             
            $result = mysql_query($query)
                or die(mysql_error());
            
        }
        echo  "The Schedule has been successfully posted ";
    
   } 
?>
any help would be appreciated, thanks
Last edited by Benjamin on Fri May 29, 2009 10:59 am, edited 1 time in total.
Reason: Changed code type from text to php.
t_rob76
Forum Newbie
Posts: 2
Joined: Thu May 28, 2009 11:05 pm

Re: Checking mysql date against php date

Post by t_rob76 »

after several hours using mysql_fetch_array and print_r and var_dump, I found the problem, it was in the select statement.
I think i was comparing the string $schddate, instead of the value or something. anyway, here is the difference

OLD CODE

Code: Select all

 
$query = "SELECT schdDate FROM schedule " .
 "WHERE schdDate =[color=#FF0000] " . $schddate . ";";[/color]
$result = mysql_query ($query)
or die(mysql_error());
 
NEW IMPROVED AND BETTER CODE

Code: Select all

 
$query = "SELECT schdDate FROM schedule " . 
             "WHERE schdDate =[color=#FF0000]  '$schddate' ;";[/color]
    $result = mysql_query ($query)
        or die(mysql_error());
    
 
 
Post Reply