English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Manual

PHP time() Function Usage and Examples

PHP Date & Time Functions Manual

The time() function returns the current Unix timestamp

Definition and Usage

 Returns the number of seconds since the Unix epoch (Greenwich Mean Time 1970 Year 1 Month 1 00:00:00) to the current time.

Syntax

time(void)

Parameter

This function does not accept any parameters

Return value

The PHP time() function returns an integer value representing the number of seconds between the Unix epoch (Greenwich Mean Time

PHP version

This function was originally introduced in PHP version4introduced and can be used in all higher versions.

Online example

The following example demonstratestime()The function returns the current time as a Unix timestamp and formats it as a date:

<?php
   $time = time();
   print("Current timestamp: ". $time);
    echo"<br>";
   echo(date("Y-m-d", $time));
?>
Test and see‹/›

Output result

Current timestamp: 1596166218
2020-07-31

Online example

The following example retrieves the sunrise and sunset times for the current date-

<?php
   $dateString = '11-06-2012 12:50 GMT';
   print("Date: ". date("D M d Y"));
   print("\n");
   print("Sunset time: ");
   print(date_sunset(time(), SUNFUNCS_RET_STRING,38.4,-9,90,1));
   print("\n");
   print("Sunrise time: ");
   print(date_sunrise(time(), SUNFUNCS_RET_STRING,38.4,-9,90,1));
?>
Test and see‹/›

Output result

Date: Fri May 08 2020
Sunset time: 20:31
Sunrise time: 06:33

Online example

The following example adds and subtracts from the current timestamp23days,12hours and30 minutes, and print the result-

<?php
   $timestamp1 = time() - (23*12*30);
   print_r($timestamp1); 
   print("\n");
   $timestamp2 = time() + (23*12*30);
   print_r($timestamp2); 
?>
Test and see‹/›

Output result

1588935317
1588951877

Online example

Add1Week time

<?php
   $nextWeek = time() + (7 * 24 * 60 * 60);   
   echo 'Now:'. date('Y-m-d') . "\n";
   echo 'Next week: '. date('Y-m-d', $nextWeek) . "\n";
?>
Test and see‹/›

This produces the following result-

Now: 2005-03-30
Next week: 2005-04-06