Generate and paste date into MySQL database using php

I used the following code to insert yesterday's date into my_table of a MySQL database. He worked great at WAMP and MAMP. But this does not work on my host. What 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()); 
+4
source share
2 answers

Perhaps your host does not support the DateTime () function ...

Try using the date () and strtotime () functions :

$today = time();
$yesterday = date('Y-m-d H:i:s', strtotime('-1 day', $today));
+4
source

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)

+1
source

Source: https://habr.com/ru/post/1527302/


All Articles