| | |
if statement with dates
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 0
hello, im using a delete function but some how i want to use an if statement; for example if the current server date matches the date from a row from a database table and if the date is 2days from the current date it can't be delete.
PHP Syntax (Toggle Plain Text)
Delete from House where rooms='10';
i prefer to use php rather than hard coding in mysql as date var can be set and change easier
## php
## mysql
Delete from House where rooms='10' and roomdate < DATE_SUB(CURDATE(), INTERVAL 2 DAY)
## php
$del_date = date('Y-m-d', strtotime(' + 2 days')); Delete from House where rooms='10' and roomdate > '$del_date'## mysql
Delete from House where rooms='10' and roomdate < DATE_SUB(CURDATE(), INTERVAL 2 DAY)
Last edited by peter_budo; Apr 29th, 2008 at 9:23 am. Reason: Keep It Organized - please use [code] tags
php should be -2 days
## php
## php
$del_date = date('Y-m-d', strtotime(' -2 days')); Delete from House where rooms='10' and roomdate < '$del_date' Last edited by peter_budo; Apr 29th, 2008 at 9:25 am. Reason: Keep It Organized - please use [code] tags
nothing needs to go into sql just as long as you have a date to del from like roomdate.
Delete from House where rooms='10' and roomdate < '$del_date'
// find rooms with 10 and all room dates less than $del_date
run query
Delete from House where rooms='10' and roomdate < '2008-04-25'
house1 - 10 - 2008-04-26
house2 - 10 - 2008-04-11 [ deleted ]
house3 - 5 - 2007-04-22
house4 - 7 - 2008-04-25
house5 - 10 - 2008-04-24 [ deleted ]
house6 - 10 - 2008-04-25
$del_date = date('Y-m-d', strtotime(' -2 days')); // this makes the date 2 days ago eg. 2008-04-25Delete from House where rooms='10' and roomdate < '$del_date'
// find rooms with 10 and all room dates less than $del_date
run query
Delete from House where rooms='10' and roomdate < '2008-04-25'
house1 - 10 - 2008-04-26
house2 - 10 - 2008-04-11 [ deleted ]
house3 - 5 - 2007-04-22
house4 - 7 - 2008-04-25
house5 - 10 - 2008-04-24 [ deleted ]
house6 - 10 - 2008-04-25
Last edited by peter_budo; Apr 29th, 2008 at 9:25 am. Reason: Keep It Organized - please use [code] tags
•
•
Join Date: Mar 2008
Posts: 42
Reputation:
Solved Threads: 0
how can i add some sort of message saying you can not delete this record and a message saying deleted?? also i dont want the user to be able to input a date they only input an id and click submit and the code you have given requires a date to be entered by a user or does it?? cheers
Last edited by aran87; Apr 27th, 2008 at 5:48 pm. Reason: error
•
•
•
•
1.
$del_date = date('Y-m-d', strtotime(' -2 days')); // this makes the date 2 days ago
eg. 2008-04-25
•
•
•
•
run query
Delete from House where rooms='10' and roomdate < '2008-04-25'
house1 - 10 - 2008-04-26
house2 - 10 - 2008-04-11 [ deleted ]
house3 - 5 - 2007-04-22
house4 - 7 - 2008-04-25
house5 - 10 - 2008-04-24 [ deleted ]
house6 - 10 - 2008-04-25
---------------
PHP Syntax (Toggle Plain Text)
$del_date = date('Y-m-d', strtotime(' -2 days')); $sql = "DELETE FROM House where id='".mysql_real_escape_string($id)."' and roomdate < '$del_date'"; $query = mysql_query($sql) ; if(mysql_affected_rows()>0){ deleted }else{ not deleted }
Last edited by amigura; Apr 27th, 2008 at 6:18 pm.
![]() |
Similar Threads
- small error with dates (MySQL)
- coversion of dates from java(jsp) to sql (JSP)
- How to search between two dates using mysql or php (PHP)
- calculate the date difference between two dates exclude the saturday and sunday (Java)
- array structs, size swap functions, the amount of days between to dates (C++)
- Problems with switch statement (C++)
Other Threads in the PHP Forum
- Previous Thread: Query on how to access an array in an array?
- Next Thread: What does this do?
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner binary broken cakephp checkbox class cms code compression cron curl data database date display dropdownlist dynamic echo email eregi error execution file files folder form forms function functions google href htaccess html httppost if...loop image include insert ip javascript joomla jquery key library limit link links login mail md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote screen script search searchbox server session sessions sms sorting source space sql syntax system table tutorial update upload url validator variable video volume votedown web website youtube zend





