Michał Staniewski
Michał Staniewski

Reputation: 260

strtotime php functions outputs good time in php file but wrong after update in MySQL

Ive got such script:

mysql_connect('localhost', 'xxx', 'xxx');
mysql_select_db('xxx');

$sql = "SELECT * FROM rl_cronjobs";
$sql = mysql_query($sql);
$row = mysql_fetch_array($sql);

$sunday = $row['next_sunday'];
$today = date('Y-m-d');


if($sunday == $today){
    $sql = "DELETE * FROM xxx WHERE stala != 1";
    $sql = mysql_query($sql);
    echo $nextsunday;
    $nextsunday = strtotime("next Sunday");
    $nextsunday = date('Y-m-d', $nextsunday);   
    $sql = "UPDATE xxx SET next_sunday = $nextsunday";
    $sql = mysql_query($sql) or die (mysql_error());
    echo $nextsunday;
}

And in MySQL the value of it is: 1984 I need it to be normal date of next sunday

The type of MySQL table row is varchar because when i set tu date it doesn't update. Any help?

Upvotes: 1

Views: 145

Answers (2)

Marc B
Marc B

Reputation: 360782

Don't store dates in varchar fields. Mysql has date, datetime, and timestamp fields for such things, and using them opens a world of new possibilities. E.g. your "next sunday" calculations can be done as simply as:

UPDATE xxx SET next_sunday = NOW() + INTERVAL (8 - DAYOFWEEK(NOW())) DAY

without ever involving the PHP time functions. Note that this particular function will always pick the real next sunday, so if you run this code on a Sunday, you'll get the following weeks' sunday, not "today", e.g.:

'2012-12-08' (Saturday) -> '2012-12-09'
'2012-12-09' (Sunday)   -> '2012-12-16'
'2012-12-10' (Monday)   -> '2012-12-16'

As well, note that you're trying to echo $nextsunday before it's been defined.

Upvotes: 1

complex857
complex857

Reputation: 20753

Looks like you missing some quotes " or ' in your update query. You probably meant to write:

$sql = "UPDATE xxx SET next_sunday = '$nextsunday'";

sidenote: The myslq_* functions are deprecated, you shouldn't use them in new code anymore.

Upvotes: 1

Related Questions