Home > Mobile >  Add day to empty database time
Add day to empty database time

Time:10-18

I have this code

$Date = $db_date;
$ss = date('Y-m-d', strtotime($Date. '   1 days'));

$x = $con->prepare("UPDATE ssss SET ss=? WHERE sss=?");
$x->bind_param("ss", $ss, $sss);
$x->execute();

The code adds 1 day to current database time if it's not empty, for example 2021-10-17, so it will be 2021-10-18. However, if the database time is empty (0000-00-00), it does nothing. I want to add 1 day to current day if the database time is empty.

CodePudding user response:

When the date is all zeroes, now can be used in the strtotime call as the current day:

$Date = $db_date;

if ($Date == '0000-00-00') {
    $ss = date('Y-m-d', strtotime('now   1 days'));
} else {
    $ss = date('Y-m-d', strtotime($Date. '   1 days'));
}

See this fiddle example with the date set to 0000-00-00.

CodePudding user response:

You can do this using simple SQLquery:

UPDATE test SET sss = DATE_ADD(IFNULL(sss, CURDATE()), INTERVAL 1 DAY);

Test it on SQL fiddle

  • Related