4

I used following code to insert the yesterday date into my_table of MySQL database. It worked fine in WAMP and MAMP. But it doesn't work in my host. What would be the reason Please help...

$dt = new DateTime();

$d = date_add($dt,date_interval_create_from_date_string("-1 days"));

$date = $d->format('Y-m-d');

$import="INSERT into my_table (date) Values('$date')”;

mysql_query($import) or die(mysql_error()); 
2
  • Please define "Doesn't work" Commented Feb 17, 2014 at 11:09
  • where is the code of this function? date_interval_create_from_date_string() Commented Feb 17, 2014 at 11:11

2 Answers 2

4

Maybe your host doesn't support the DateTime() function ...

Try with date() and strtotime() functions :

$today = time();
$yesterday = date('Y-m-d H:i:s', strtotime('-1 day', $today));
Sign up to request clarification or add additional context in comments.

2 Comments

Of cause that was the problem and this works fine. Thank you!
You're welcome, for more informations on strtotime() : php.net/manual/en/function.strtotime.php
1

Remove and use "

$date = new DateTime();
$date->add(DateInterval::createFromDateString('yesterday'));
$YesterdayDate = $date->format('Y-m-d H:i:s');

$import="INSERT into my_table (`date`) Values('$YesterdayDate')";
                                               .....^

NOTE: Use mysqli_* OR PDO functions instead of mysql_* functions(deprecated)

2 Comments

Thank you but here it works both the way. So this wasn't the problem.
@user3206125, Thanks to pointing. For the better practice you need to use "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.