PHP - SPLessons

PHP Date

Chapter 23

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PHP Date

PHP Date

shape Description

PHP Date chapter gives introduction to Timestamp initially and explains clearly about Time and Data Functions. Normally, Timestamp in UNIX will be the number of seconds that have been passed since January 1, 1970. Currently, the timestamp is 1467952558.But for every second, this time keep changing which increases the time number when the page is refreshed.

PHP date() function follows and formats the timestamp with high readability to give exact date and time.

shape Syntax

date(format,timestamp)

formatSpecifies the format of the timestamp and is mandatory.
timestampSpecifies a timestamp. it’s default value is current date and time and is optional.

Characters used commonly:

Characters, like .,  /  or  - also can be inserted in between the characters to add extra format to date() function.

shape Example

<!DOCTYPE html>
<html>
	<body>
	 
		<?php 
			echo "Today Date Pattern 1 : " . date("Y/m/d") . "<br /> ";
			echo "Today Date Pattern 2 : " . date("Y.m.d") . "<br /> "; 
			echo "Today Date Pattern 3 : " . date("Y-m-d") . "<br /> ";
			echo "Week of the Today : " . date("l"); 
		?>
	 
	</body>
</html>

Output:

PHP Time

shape Description

The characters that are most commonly used for time are:

shape Example

<!DOCTYPE html>
<html>
	<body>
		<?php echo "The time is " . date("h:i:sa"); ?>
	</body>
</html>

Output:

Strtotime() Function

shape Description

The strtotime() function takes human readable language datetime description like English and converts it into a timestamp. By this,it is eady to determine “last Friday” or “past week” without the help of time() function and large math code.

shape Example

<!DOCTYPE html>
<html>
	<body>
		<?php
			  $a=strtotime("11:00am June 07 2016");
			  echo "Created date is " . date("Y-m-d h:i:sa", $a)."<br/>";
			  echo strtotime("now") . "<br />";
			  
			  echo strtotime("tomorrow") . "<br />";
			  echo strtotime("yesterday") . "<br />";
			  echo strtotime("10 September 2000") . "<br />";
			  echo strtotime("+1 day") . "<br />";
			  echo strtotime("+1 week") . "<br />";
			  echo strtotime("+1 week 2 days 4 hours 2 seconds") . "<br />";
			  echo strtotime("next Thursday") . "<br />";
			  echo strtotime("last Monday") . "<br />";
			  echo strtotime("4pm + 2 Hours") . "<br />";
			  echo strtotime("now + 2 fortnights") . "<br />";
			  echo strtotime("last Monday") . "<br />";
			  echo strtotime("2pm yesterday") . "<br />";
			  echo strtotime("7am 12 days ago") . "<br />";
		?>
	</body>
</html>

Summary

shape Points

  • Timestamp is number of seconds that have been passed since January 1, 1970.
  • Characters used in date function are d,m,Y,l,/,. and -.
  • Characters used in date function are h,s,i and a.
  • strtotime() converts human readable language datetime description to timestamp.