Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

date / time condition

Tags:

date

php

time

mysql

I have this code, and i want to execute the sql query only if the requested operation is made on specific date. ( for example if i set the date 4.05.2015, the query will be made only on that day, if i try to make the query on 5.05.2015, nothing will happen . Thx in advice

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);
like image 281
rico Avatar asked Mar 12 '26 21:03

rico


2 Answers

$date = "2015-05-05";
$today = date('Y-m-d');

if ($date == $today){

$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);

}

You will want to wrap your code in a statement that actually checks whether if the submission date is valid:

$check_date = (some date as a string);
$check_date = date('Y-m-d', strtotime($check_date));
$current_date = date('Y-m-d');

if ($check_date == $current_date) {
$con = mysql_connect($db_hostname,$db_username,$db_password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($db_database, $con);

$sql2 = "UPDATE `nume` SET `venit3` = '".$_GET['box3']."' WHERE `nr` = '".$_GET['nr']."';";
mysql_query($sql2);
}

Bear in mind that this example assumes that $check_date is initially a string.

like image 28
grill Avatar answered Mar 14 '26 09:03

grill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!