简体   繁体   中英

PHP: Update a date value?

What is the easiest way to update a date?

In my database I have a field called mydate that is setup as a timestamp datatype. (It as to be this datatype).

My dates are listed as either:

2013-07-25 00:00:00

or

0000-00-00 00:00:00

Meaning they either have a data or just zeroes, but never empty.

Edit: Thank you all! I sort of combined all that you said and made it work.

Now lets say I wanted to increase any give date with 10 days how would I accomplish that?

The first one, July 25th 2013 would become 2013-08-04 00:00:00 and the other would become 2013-06-18 00:00:00 (at the time of posting it's June the 8th).

How to?

Something along these lines:

if(mydate_from_db > present_date){
mydate_from_db = mydate_from_db + day('10')
}
else{
mydate_from_db = present_date + day('10')
}

Update mysql with mydate where relevant.

With PHP you can do something like:

if($mydate_from_db > $present_date){
    $mydate_from_db = strtotime($mydate_from_db, '+ 10 days');
} else {
    $mydate_from_db = strtotime($present_date, '+ 10 days');
}

Or you can do it with mysql like:

UPDATE yourtable SET mydate = IF(mydate = '0000-00-00 00:00:00', '2013-06-18 00:00:00', DATE_ADD(mydate, INTERVAL 10 DAY));

You can do it in MySql directly:

UPDATE 
    my_table 
SET 
    my_date = my_date + INTERVAL 10 DAY 
WHERE 
    DATE(my_date) >= '2013-06-01' 
 if(mydate_from_db > present_date)
  {
    $date = strtotime(mydate_from_db );
    $date = strtotime("+10 day", $date);
    echo date('M d, Y', $date);
   }
 else
  {
 $date=date();
 $date = strtotime("+10 day", $date);
 echo date('M d, Y', $date);
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM