How to get current date and time in PHP like MySQL's NOW()?
Hello everyone! I'm just starting to learn PHP and switched from MySQL, where I often used the NOW() function to insert the current date and time into tables. For example, in SQL I wrote:
```sql
INSERT INTO posts (title, created_at) VALUES ('My post', NOW());
```
This worked perfectly. Now I'm trying to do the same in PHP when saving data to the database. I'm writing code like this:
```php
$title = $_POST['title'];
$sql = "INSERT INTO posts (title, created_at) VALUES ('$title', NOW())";
```
But I've been told that it's bad practice to use SQL functions inside a PHP query because it can lead to timezone issues and mismatches between the server and database time. Also, if I want to check the time before insertion or format it in PHP, I won't be able to.
Question
No answers yet.
Log in to leave an answer