Lesson 4: Working with time and dates
Ak Patel
---
ime 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:With different parameters, the
- date("y")
- Returns the current year from a date - with today's date, it returns: 14
- date("m")
- Returns the current month from a date - with today's date, it returns: 01
- date("n")
- Returns the current month from a date without leading zeroes ( eg. "1" instead of "01") - with today's date, it returns: 1
- date("F")
- Returns the current month name from a date - with today's date, it returns: January
- date("d")
- Returns the current day of the month from a date - with today's date, it returns: 10
- date("l")
- Returns the name of the current weekday from a date - with today's date, it returns: Friday
- date("w")
- Returns the current day of the week from a date - with today's date, it returns: 5
- date("H")
- Returns the current hour from a time - with the current time, it returns: 13
- date("i")
- Returns the current minute from a time - with the current time, it returns: 24
- date("s")
- Returns the current second from a time - with the current time, it returns: 39
<html> <head> <title>Time and date</title> </head> <body> <?php echo "<p>Today it's " . date("l") . "</p>"; ?> </body> </html>
The time is 1389356679
And now hold on... because now it becomes a little nerdy! The function<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>
By default, the
<html> <head> <title>time and date</title> </head> <body> <?php echo "<p>January 1, 1970 was a " . date("l",0) . "</p>"; ?> </body> </html>
The syntax for
<html> <head> <title>time and date</title> </head> <body> <?php echo mktime (2,56,0,7,21,1969); ?> </body> </html>
We can now put this together with the
<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 likeThe 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>
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?