Php get the timestamp

PHP time

Summary: in this tutorial, you’ll learn how to work with the PHP time() function to get the current timestamp in the local timezone.

Introduction to Unix timestamps

Computers store a date and time as a UNIX timestamp or a timestamp in short.

A timestamp is an integer that refers to the number of seconds between 1970-01-01 00:00:00 UTC (Epoch) and the date and time to be stored.

Computers store dates and times as timestamps because it is easier to manipulate an integer. For example, to add one day to a timestamp, it simply adds the number of seconds to the timestamp.

PHP provides some helpful functions that manipulate timestamps effectively.

Getting the current time

To get the current time, you use the time() function:

function time(): intCode language: PHP (php)

The time() function returns the current UNIX timestamp since Epoch (January 1 1970 00:00:00 GMT). For example:

 echo time(); // 1626752728Code language: PHP (php)

The return value is a big integer that represents the number of seconds since Epoch. To make the time human-readable, you use the date() function. For example:

 $current_time = time(); echo date('Y-m-d g:ia', $current_time) . '
'
;
Code language: PHP (php)
2021-07-13 5:47amCode language: PHP (php)

The date() function has two parameters.

  • The first parameter specifies the date and time format. Here’s a complete list of valid date and time formats.
  • The second parameter is an integer that specifies the timestamp.

Since the time() function returns a timestamp, you can add seconds to it.

Adding to / subtracting from a timestamp

The following example shows how to add a week to the current time:

 $current_time = time(); // 7 days later $one_week_later = $current_time + 7 * 24 * 60 * 60; echo date('Y-m-d g:ia',$one_week_later);Code language: PHP (php)

In this example, we add 7 days * 24 hours * 60 minutes * 60 seconds to the current time.

Also, you can represent a time in the past by subtracting a number of seconds from the current time. For example:

 $current_time = time(); // 1 day ago $yesterday = $current_time - 24 * 60 * 60; echo date('Y-m-d g:ia',$yesterday);Code language: PHP (php)

timezone

By default, the time() function returns the current time in the timezone specified in the PHP configuration file (php.ini).

To get the current timezone, you can use the date_default_timezone_get() function:

 echo echo date_default_timezone_get(); Code language: PHP (php)

To set a specific timezone, you use the date_default_timezone_set() . It’s recommended that you use the UTC timezone.

The following shows how to use the date_default_timezone_set() function to set the current timezone to the UTC timezone:

 date_default_timezone_set('UTC');Code language: PHP (php)

Making a Unix timestamp

To make a Unix timestamp, you use the mktime() function:

mktime( int $hour, int|null $minute = null, int|null $second = null, int|null $month = null, int|null $day = null, int|null $year = null ): int|falseCode language: PHP (php)

The mktime() function returns a Unix timestamp based on its arguments. If you omit an argument, mktime() function will use the current value according to the local date and time instead.

The following example shows how to use the mktime() function to show that July 13, 2020, is on a Tuesday:

 echo 'July 13, 2021 is on a ' . date('l', mktime(0, 0, 0, 7, 13, 2021));Code language: PHP (php)

Summary

  • Use the time() function to return the current timestamp since Epoch in local timezone.
  • Use the date_default_timezone_set() function to set a specific timezone.
  • Use the date() function to format the timestamp.
  • Use mktime() function to create a timestasmp based on the year, month, day, hour, minute, and second.

Источник

PHP Timestamp

PHP provides several date-time functions to perform required operations with temporal data. Now, we are going to see about PHP timestamp functions. The timestamp is the value represented as seconds calculated, since UNIX Epoch, January 1, 1970, and also called as UNIX timestamp.

In PHP, it includes several functions to work with a timestamp. In this article, we are going to see how the following list of timestamp-related functionalities is obtained by using PHP date-time functions.

Getting Current Timestamp in PHP

We can get the current timestamp value in three possible ways with the help of the PHP core functions described here.

time()

This is the simple and widely used PHP function to get the current timestamp value. It requires no arguments to be sent for returning the expected resultant UNIX timestamp. The usage of this simple function is shown in the example PHP program below.

strtotime()

This function is mainly used to get timestamp value from the given string representing the date value. PHP provides a list of supported strings to be passed as an argument of this function to denote date values. For example, “Tuesday last week”, “+1 week”, “21 November 2008” and etc.

similarly, for getting the current timestamp value, we need to provide the string as “now” representing the current date and time value. So, the code will be as follows.

While invoking strtotime() by passing improper string data which is not supported by PHP, this function will return false.

mktime()

This function is also used to get UNIX timestamps but requires a set of parameters denoting date components, like an hour, minute, second, month, day, and year, in the same order specified here. And also has an optional flag representing the daylight saving time state.

And for getting, the current timestamp, we have to use the PHP date() function within this function, with the corresponding parameter for getting the current hour, and minute, in the required order. For example,

microtime()

All the above PHP timestamp functions will return only the 10-digit timestamp value. But, while executing microtime() function, it will return the number of seconds elapsed since, the UNIX Epoch, and also, the number of microseconds elapsed since second value returned by this function.

microtime() is same as time() functions, which doesn’t require any parameter, but, there is an optional parameter $get_as_float. This parameter will accept boolean values for it. If it is TRUE, then microtime() will return a float value representing an accurate timestamp, otherwise, will return the “microseconds seconds” formatted string. And the code is,

date()

PHP supports several date format characters denoting components of the date value. So, we can use this list of date formatting characters to get date components or to format dates if required, using given temporal data.

From the list of those strings, U denotes the UNIX timestamp value. So, for the date() function, we should specify U as an argument to get the current timestamp value. For example,

Date/Time to Timestamp Conversion

strtotime() and mktime() functions are also used to convert specified date into the form of a timestamp.

For using strtotime(), we need to pass the date with any one of PHP-supported date formats, for example, dd/mm/yyyy, mm/dd/yyyy and etc. And, for using mktime(), we need to explode the given date and send the exploded components to this function.

And, we can perform the reverse, that is, converting timestamp value to date, by the use of date() function. For that, we should specify the required date format as the first parameter, and timestamp as the second one. For example,

  • These operations can also be performed in the object-oriented style of programming, with the PHP functions defined under the DateTime class interface.
  • On the other hand, certain PHP timestamp functions are in a procedural style, which is the alias of class constructors defined for the PHP DateTime object model.

Comments to “PHP Timestamp”

Great article. I really appreciate your help that’s really exactly what I was looking for, thank’s for that!

Источник

Читайте также:  Php book pdf file
Оцените статью