PHP date/time FAQ: How do I create a date in the proper format to insert a SQL Timestamp field into a SQL database? Show
Note: You might not need to create a PHP dateFirst off, you may not need to create a date in PHP like this. If you’re using plain old PHP and a database like MySQL, you can use the SQL INSERT INTO projects (user_id, name, last_updated, date_created) VALUES (5, 'alvin', now(), now()); I just tested this with PHP and MySQL, and it works fine. So that’s one way to populate a SQL timestamp field in a SQL INSERT query. Creating a PHP timestamp variableHowever, if you want to do this all in PHP (or need to, depending on what framework you're working with), you can get the current date and time in the proper format using just PHP, like this: $timestamp = date('Y-m-d H:i:s'); If you print this out, your 2019-10-02 18:41:17 You can then use this formatted timestamp string in a PHP MySQL insert.
A Drupal 7 SQL INSERT with Timestamp exampleAlthough this isn't a standard off-the-shelf PHP/MySQL $project = new stdClass(); $project->user_id = get_user_id(); $project->project_count_type = $form_state['values']['type']; $project->name = $form_state['values']['name']; $project->description = $form_state['values']['description']; # get the current time in the proper format for a sql timestamp field $timestamp = date('Y-m-d H:i:s'); # new drupal 7 style insert $id = db_insert('projects') ->fields(array( 'user_id' => $project->user_id, 'project_count_type' => $project->project_count_type, 'name' => $project->name, 'description' => $project->description, 'last_updated' => $timestamp, 'date_created' => $timestamp )) ->execute(); As you can see in the lines I’ve made bold, I’m inserting my PHP timestamp variable into two SQL fields. Getting a timestamp for some other date and timeNote that the PHP $timestamp = date('Y-m-d H:i:s', mktime(0, 0, 0, 7, 1, 2000)); Here are some other PHP $tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y")); $lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y")); $nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1); I pulled those examples from the PHP date page. Please see that page for more information on creating other dates and times (I'm mostly just worried about "now" at this moment). PHP SQL Timestamp insertsI hope these timestamp examples have been helpful. As you've seen, you can generally just use the SQL 'NOW()' function to insert into a SQL timestamp field, but if that doesn't work for some reason, you can also create a timestamp field in the proper format using just PHP and the date function. View Discussion Improve Article Save Article View Discussion Improve Article Save Article Problem: Convert timestamp to readable date/time in PHP Example 1:
Output: 11/10/2018 09:51:07 Example 2:
Output: Current Time in formatted form Reference: http://php.net/manual/en/function.date.php PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. How do I get a timestamp?Getting the Current Time Stamp
const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.
How can I insert current date and time in PHP?Answer: Use the PHP date() Function
You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
How does PHP time work?The time() function is a built-in function in PHP which returns the current time measured in the number of seconds since the Unix Epoch. The number of seconds can be converted to the current date using date() function in PHP.
How can I get UTC in PHP?Get UTC Time in PHP. Use gmdate() to Get UTC Time.. Use strtotime() to Get UTC Time.. Use date() to Get UTC Time.. Use date_default_timezone_set() to Get UTC Time.. Use DateTime Object to Get UTC Time.. |