How can i timestamp in php?

PHP date/time FAQ: How do I create a date in the proper format to insert a SQL Timestamp field into a SQL database?

Note: You might not need to create a PHP date

First 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 now() function to insert data into a SQL timestamp field like this:

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 variable

However, 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 $timestamp field will now contain contents like this:

2019-10-02 18:41:17

You can then use this formatted timestamp string in a PHP MySQL insert.

Note: Thanks to the commenters below who suggest using H:i:s instead of G:i:s.

A Drupal 7 SQL INSERT with Timestamp example

Although this isn't a standard off-the-shelf PHP/MySQL INSERT statement, here's what a SQL INSERT query looks like when I use this with Drupal 7:

$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 time

Note that the PHP date function defaults to the current date and time, which is exactly what I need for my purposes here. If you need to create a formatted timestamp field for some other date and time, you can do that something like this:

$timestamp = date('Y-m-d H:i:s', mktime(0, 0, 0, 7, 1, 2000));

Here are some other PHP mktime examples:

$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 inserts

I 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

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Problem: Convert timestamp to readable date/time in PHP
    Solution: This can be achieved with the help of date() function, which is an inbuilt function in PHP can be used to format the timestamp given by time() function. This function returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.

    Example 1:

    <?php

    echo date('m/d/Y H:i:s', 1541843467);

    ?>

    Output:

    11/10/2018 09:51:07
    

    Example 2:

    <?php

    echo date('m/d/Y H:i:s', time());

    ?>

    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..