Login | Register | FAQ
Anonymous

Lesson 4: Working with time and dates

In this lesson, we will try to look at the many different options for working with time and dates in PHP. We went through some very simple examples in the previous lesson mostly to show you what PHP is. In this lesson, we will take a closer look at the documentationdate function.

Time and date functions

PHP provides a wide range of funtions in relation to time and date. In this lesson, we will look at the most important of these functions: documentationdate.

With different parameters, the documentationdate function can return the current date / time in many different formats. Some of the most useful parameters are:

date("y")
Returns the current year from a date - with today's date, it returns: 17
date("m")
Returns the current month from a date - with today's date, it returns: 04
date("n")
Returns the current month from a date without leading zeroes ( eg. "1" instead of "01") - with today's date, it returns: 4
date("F")
Returns the current month name from a date - with today's date, it returns: April
date("d")
Returns the current day of the month from a date - with today's date, it returns: 27
date("l")
Returns the name of the current weekday from a date - with today's date, it returns: Thursday
date("w")
Returns the current day of the week from a date - with today's date, it returns: 4
date("H")
Returns the current hour from a time - with the current time, it returns: 14
date("i")
Returns the current minute from a time - with the current time, it returns: 49
date("s")
Returns the current second from a time - with the current time, it returns: 10

This example illustrates the use of the documentationdate function:


	<html>
	<head>
	<title>Time and date</title>

	</head>
	<body>

	<?php 
	
	echo "<p>Today it's " . date("l") . "</p>";

	?>
	
	</body>
	</html>
	
	

The time is 1493304550

And now hold on... because now it becomes a little nerdy! The function documentationtime() returns the current time as the number of seconds since January 1, 1970, 12:00 PM, GMT.


	<html>
	<head>
	<title>time and date</title>
	</head>
	<body>

	<?php   

	echo "<p>It's been exactly " . time() . " seconds since January 1, 1970, 12:00 PM, GMT </ p> ";

	?>

	</body>
	</html>
	
	

Time expressed in the number of seconds since January 1, 1970, 12:00 PM GMT is a so-called "timestamp" (UNIX timestamp) and is quite useful when you work with dates/times in the future or the past.

By default, the documentationdate function uses the current timestamp (i.e. the current value of documentationtime()). But with an extra parameter you can specify a different time stamp and thus work with the future or the past. In the example below, we set the timestamp to 0 seconds from January 1, 1970 12:00 PM, GMT. Thereby we can check what day of week January 1, 1970 was.


	<html>
	<head>
	<title>time and date</title>
	</head>
	<body>

	<?php 
	
	echo "<p>January 1, 1970 was a " . date("l",0) . "</p>";

	?>

	</body>
	</html>
	
	

Unless you are a mathematical genius, it quickly becomes complicated to count the number of seconds since January 1, 1970 to a specific time in the future or past. But here you can get help from another nifty function: documentationmktime, which does the calculations for you.

The syntax for documentationmktime is (hour, minute, second, month, day, year). The example below converts the time of the first step on the Moon (July 21, 1969, 02:56):


	<html>
	<head>
	<title>time and date</title>
	</head>
	<body>

	<?php  
	
	echo mktime (2,56,0,7,21,1969);

	?>

	</body>
	</html>
	
	

Notice that it's returning a negative number as the date is earlier than January 1, 1970.

We can now put this together with the documentationdate function and find out which weekday this historic day took place.


	<html>
	<head>
	<title>time and date</title>
	</head>
	<body>

	<?php
	
	echo date("l", mktime(2,56,0,7,21,1969));
	
	?>
	
	</body>
	</html>
	
	

What can you use it for?

All this may seem a bit theoretical at this stage. After all, what on earth can you use a function like documentationtime() for? More importantly, when will you learn something you can actually use on your pages?

The answer is that what you learn here are building blocks - the only limitations to what you can build are your creativity and imagination! I would venture to say that you have already learned more than you think. For example, do you think you can make a website with different background images each day of the week and that works in all browsers?

Sure you can! Look at this example:


	<html>
	<head>
	<title>time and date</title>
	</head>

	<body background="background_<?php echo date("w"); ?>.png">

	</body>
	</html>
	
	

The example above, with a dynamic background image, simply requires that you make seven images and name them background_1.png, background_2.png, background_3.png, etc.

If a user then enters your site on a Tuesday, the site will have background_2.png as background, and the next day, background_3.png. Easy and simple!

In the next lesson, you will be introduced to new building blocks that can be used to make loops and repetitions in your codes.

PHP is fun, don't you think?


Related topics in the PHP ForumRepliesViews
can't figure it out what i am doing wrong in date and time2724
PHP-Lesson 4: date&time documentation!46888
want data from two different table according to their date c17424
PHP Time & Date16524
Dynamic Date/Time Clock in PHP733198

+ Post a new topic


<< Lesson 3: Your first PHP page

Lesson 5: Loops >>