What is php time function?

[PHP 4, PHP 5, PHP 7, PHP 8]

timeReturn current Unix timestamp

Description

time[]: int

Note:

Unix timestamps do not contain any information with regards to any local timezone. It is recommended to use the DateTimeImmutable class for handling date and time information in order to avoid the pitfalls that come with just Unix timestamps.

Parameters

This function has no parameters.

Return Values

Returns the current timestamp.

Examples

Example #1 time[] example

The above example will output something similar to:

Notes

See Also

  • DateTimeImmutable
  • date[] - Format a Unix timestamp
  • microtime[] - Return current Unix timestamp with microseconds

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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.

    Syntax:

    int time[]

    Parameter: This function does not accepts any parameters as shown above.

    Return Value: This function returns the current time measured in the number of seconds since the Unix Epoch.

    Note: All output of programs corresponds to the date when the article was written.

    Below programs illustrate the time[] function:

    Program 1: The program below prints the current time in term of seconds.

    Output:

    1525376494
    

    Program 2: The program below prints the current time in date format.

    Output:

    2018-05-03 
    

    The time[] function in PHP returns the current time as a Unix timestamp. It returns the current time measured in the number of seconds since the Unix Epoch [January 1 1970 00:00:00 GMT].

    Syntax

    time[]

    Parameters

    • NA

    Return

    The time[] function returns the current time measured in the number of seconds since the Unix Epoch [January 1 1970 00:00:00 GMT].

    Example

    The following is an example −

     Live Demo

    Output

    The following is the output −

    Now: 2018-10-11
    Next Week: 2018-10-18

    Example

    Let us see another example −

     Live Demo

    Output

    The following is the output −

    1539245504
    2018-10-11

    Updated on 24-Dec-2019 10:19:25

    • Related Questions & Answers
    • Working with Time in PHP/ MySQL?
    • file_exists[] function in PHP
    • basename[ ] function in PHP
    • chgrp[]function in PHP
    • chmod[] function in PHP
    • chown[] function in PHP
    • clearstatcache[] function in PHP
    • copy[] function in PHP
    • delete[] function in PHP
    • dirname[]function in PHP
    • disk_free_space[] function in PHP
    • disk_total_space[] function in PHP
    • diskfreespace[] function in PHP
    • fclose[] function in PHP
    • feof[] function in PHP

    PHP date function is an in-built function that simplify working with date data types. The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.

    In this tutorial, you will learn date and time function in PHP-

    • PHP Date Syntax & Example
    • What is a TimeStamp?
    • Getting a list of available time zone identifiers
    • PHP set Timezone Programmatically
    • PHP Mktime Function
    • PHP Date function
    • Time parameters
    • Day parameters
    • Month Parameters
    • Year Parameters

    PHP Date Syntax & Example

    PHP Date the following basic syntax

    HERE,

    • “date[…]” is the function that returns the current timestamp in PHP on the server.
    • “format” is the general format which we want our output to be i.e.;
      • “Y-m-d” for PHP date format YYYY-MM-DD
      • “Y” to display the current year
      • “[timestamp]” is optional. If no timestamp has been provided, PHP will get the current PHP date time on the server.

    Let’s look at a basic example that displays the current year.

    Output:

    2018
    


    What is a TimeStamp?

    A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time [GMT].

    The value returned by the time function depends on the default time zone.

    The default time zone is set in the php.ini file.

    It can also be set programmatically using date_default_timezone_set function.

    The code below displays the current time stamp

    Assuming you saved the file timestamp.php in phptuts folder, browse to the URL //localhost/phptuts/timestamp.php

    Note: the value of the timestamp PHP is not a constant. It changes every second.


    Getting a list of available time zone identifiers

    Before we look at how to set the default time zone programmatically, let’s look at how to get a list of supported time zones.

    HERE,

    • “$timezone_identifiers = DateTimeZone::listIdentifiers[];” calls the listIdentifiers static method of the DateandTime Zone built in class.The listIdentifiers method returns a list of constants that are assigned to the variable $timezone_identifiers.
    • “foreach{…}” iterates through the numeric array and prints the values.

    Assuming you saved the file list_time_zones.php in phptuts folder, browse to the URL //localhost/phptuts/list_time_zones.php


    PHP set Timezone Programmatically

    The date_default_timezone_set function allows you to set the default time zone from a PHP script.

    The set time zone will then be used by all date in PHP function scripts. It has the following syntax.

    HERE,

    • “date_default_timezone_set[]” is the function that sets the default time zone
    • “string $timezone_identifier” is the time zone identifier

    The script below displays the time according to the default time zone set in php.ini.

    It then changes the default time zone to Asia/Calcutta and displays the time again.

    Assuming you have saved the file set_time_zone.php in the phptuts folder, browse to the URL //localhost/phptuts/set_time_zone.php


    PHP Mktime Function

    The mktime function returns the timestamp in a Unix format.

    It has the following syntax.

    HERE,

    • “mktime[…]” is the make PHP timestamp function
    • “hour” is optional, it is the number of hour
    • “minute” is optional, it is the number of minutes
    • “second” is optional, it is the number of seconds
    • “month” is optional, it is the number of the month
    • “day” is optional, it is the number of the day
    • “year” is optional, it is the number of the year
    • “is_dst” is optional, it is used to determine the day saving time [DST]. 1 is for DST, 0 if it is not and -1 if it is unknown.

    Let’s now look at an example that creates a timestamp for the date 13/10/2025 using the mktime function.

    HERE,

    • “0,0,0” is the hour, minute and seconds respectively.
    • “13” is the day of the month
    • “10” is the month of the year
    • “2025” is the year

    Output:

    1760328000
    

    PHP Date function reference

    The table below shows the common parameters used when working with the PHP date functions.

    PHP Time parameters

    Parameter Description Example
    “r” Returns the full date and time
    
    
    “a”,”A” Returns whether the current time is am or pm, AM or PM respectively
    
    
    “g”,”G” Returns the hour without leading zeroes [1 to 12], [0 to 23] respectively
    
    
    “h”,”H” Returns the hour with leading zeros [01 to 12],[00 to 23] respectively
    
    
    “i”,”s” Returns the minutes/seconds with leading zeroes [00 to 59]
    
    

    Day parameters

    Parameter Description Example
    “d” Returns the day of the month with leading zeroes [01 to 31]
    
    
    “j” Returns the day of the month without leading zeroes [1 to 31]
    
    
    “D” Returns the first 3 letters of the day name [Sub to Sat]
    
    
    “l” Returns day name of the week [Sunday to Saturday]
    
    
    “w” Returns day of the week without leading zeroes [0 to 6] Sunday is represent by zero [0] through to Saturday represented by six [6]
    
    
    “z” Returns the day of the year without leading spaces [0 through to 365]
    
    

    Month Parameters

    Parameter Description Example
    “m” Returns the month number with leading zeroes [01 to 12]
    
    
    “n” Returns the month number without leading zeroes [01 to 12]
    
    
    “M” Returns the first 3 letters of the month name [Jan to Dec]
    
    
    “F” Returns the month name [January to December]
    
    
    “t” Returns the number of days in a month [28 to 31]
    
    

    Year Parameters

    Parameter Description Example
    “L” Returns 1 if it’s a leap year and 0 if it is not a leap year
    
    
    “Y” Returns four digit year format
    
    
    “y” Returns two [2] digits year format [00 to 99]
    
    

    Summary

    • The date function in PHP is used to format the timestamp into a human desired format.
    • The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp.
    • All PHP date[] functions use the default time zone set in the php.ini file
    • The default time zone can also be set programmatically using PHP scripts.

    What is date and time function in PHP?

    PHP Date/Time Functions.

    What is the purpose of Mktime [] function?

    The mktime[] function returns the Unix timestamp for a date. This timestamp is a long integer containing the number of seconds between the Unix Epoch [January 1 1970 00:00:00 GMT] and the time specified.

    What is the use of date function in PHP?

    The date[] function formats a local date and time, and returns the formatted date string.

    How can store time in variable in PHP?

    “how to store current date in a variable php” Code Answer's.
    $today = date["F j, Y, g:i a"]; // October 30, 2019, 10:42 pm..
    $today = date["D M j G:i:s T Y"]; // Wed Oct 30 22:42:18 UTC 2019..
    $today = date["Y-m-d H:i:s"]; // 2019-10-30 22:42:18[MySQL DATETIME format].

    Chủ Đề