• PHP Video Tutorials

PHP time() Function



Definition and Usage

The time() function calculates the number of seconds between the Epoch (January 1 1970 00:00:00 GMT) and current time, and returns it.

Syntax

time(void)

Parameters

This function does not accept any parameters

Return Values

PHP time() function returns an integer value representing the number of seconds between the Epoch and the current time.

PHP Version

This function was first introduced in PHP Version 4 and, works with all the later versions.

Example

Following example demonstrates the usage of the time() function −

Live Demo
<?php
   $time = time();
   print("Current Timestamp: ".$time);
?>

This will produce following result −

Current Timestamp: 1591606322

Example

Following example gets the sun rise and sunset time of the current date −

Live Demo
<?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));
?>

This will produce following result −

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

Example

Following example adds and removes 23 days, 12 hours and, 30 minutes from the current timestamp and prints the results −

Live Demo
<?php
   $timestamp1 = time() - (23*12*30);
   print_r($timestamp1); 
   print("\n");
   $timestamp2 = time() + (23*12*30);
   print_r($timestamp2); 
?>

This will produce following result −

1588935317
1588951877

Example

Live Demo
<?php
   $nextWeek = time() + (7 * 24 * 60 * 60);   
   echo 'Now:       '. date('Y-m-d') ."\n";
   echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>

This produces the following result −

Now: 2005-03-30
Next Week: 2005-04-06
php_function_reference.htm
Advertisements