Monday, October 5, 2020

PHP Date and Time



Hello folks! welcome back to another section of my tutorial on PHP. In this section of my PHP tutorial, we will be studying about Date and Time in PHP.

Dates & time are so much part of everyday life to the point that it becomes very easy to work with them without thinking. PHP also provides powerful tools for date arithmetic that makes handling dates very easy.

Getting Time Stamp with time() Function

PHP's time() function provides you all the information that you need about the current date and time. It requires no arguments but returns an integer.


The integer which is returned by the time() function represents the number of seconds elapsed since midnight GMT on January 1, 1970. This moment is known as UNIX epoch, and the number of seconds that have elapsed since then is referred to as a time stamp.

Example

The following below is a simple example -

<?php
   print time();
?>

Output

When the above code is executed, it will produce the following result -

1601898793

This is something that is difficult to do understand. However PHP offers excellent tools to convert a time into a form that humans are comfortable with.

Converting a Time Stamp with getdate() Function

The getdate() function optionally accepts a time stamp and then returns an associative array that contains information about the date. If you omit the time stamp, then it works with the current time stamp as returned by the time() function.

Following table list the elements contained in the array returned by getdate() function -

Sr.NoKey & DescriptionExample
1

seconds

Seconds past the minutes (0-59)

20
2

minutes

Minutes past the hour (0 - 59)

29
3

hours

Hours of the day (0 - 23)

22
4

mday

Day of the month (1 - 31)

11
5

wday

Day of the week (0 - 6)

4
6

mon

Month of the year (1 - 12)

7
7

year

Year (4 digits)

1997
8

yday

Day of year ( 0 - 365 )

19
9

weekday

Day of the week

Thursday
10

month

Month of the year

January
11

0

Timestamp

948370048

Now you have a full control over date and time. You can format this date and time in whatever format you want.

Example

Try the following example below -

<?php
   $date_array = getdate();
   
   foreach ( $date_array as $key => $val ){
      print "$key = $val<br />";
   }
	
   $formated_date  = "Today's date: ";
   $formated_date .= $date_array['mday'] . "/";
   $formated_date .= $date_array['mon'] . "/";
   $formated_date .= $date_array['year'];
   
   print $formated_date;
?>

Output

When the above code is executed, it will produce the following result -

seconds = 50
minutes = 56
hours = 12
mday = 5
wday = 1
mon = 10
year = 2020
yday = 278
weekday = Monday
month = October
0 = 1601902610
Today's date: 5/10/2020

RECOMMENDED POST: PHP preg_split() Function

Converting a Time Stamp with date() Function

The PHP's date() function returns a formatted string representing a date. You can exercise a very huge amount of control over the style that date() function returns with a string argument that you must pass to it.

date(format,  timestamp)

The PHP date() function optionally accepts a time stamp. If it is left out, then current date and time will be used. Any other date that you include in the format string that is passed to date() function will be included in the return value.

Following table below list all the codes that a format string can hold -

Sr.NoFormat & DescriptionExample
1

a

'am' or 'pm' lowercase

pm
2

A

'AM' or 'PM' uppercase

PM
3

d

Day of month, a number with leading zeroes

20
4

D

Day of week (three letters)

Thu
5

F

Month name

January
6

h

Hour (12-hour format - leading zeroes)

12
7

H

Hour (24-hour format - leading zeroes)

22
8

g

Hour (12-hour format - no leading zeroes)

12
9

G

Hour (24-hour format - no leading zeroes)

22
10

i

Minutes ( 0 - 59 )

23
11

j

Day of the month (no leading zeroes

20
12

l (Lower 'L')

Day of the week

Thursday
13

L

Leap year ('1' for yes, '0' for no)

1
14

m

Month of year (number - leading zeroes)

1
15

M

Month of year (three letters)

Jan
16

r

The RFC 2822 formatted date

Thu, 21 Dec 2000 16:01:07 +0200
17

n

Month of year (number - no leading zeroes)

2
18

s

Seconds of hour

20
19

U

Time stamp

948372444
20

y

Year (two digits)

06
21

Y

Year (four digits)

2006
22

z

Day of year (0 - 365)

206
23

Z

Offset in seconds from GMT

+5

Example
Try the following example below -

<?php
   print date("m/d/y G.i:s<br>", time());
   echo "<br>";
   print "Today is ";
   print date("j of F Y, \a\\t g.i a", time());
?>

Output
When the above code is executed, it will produce the following result -

10/05/20 13.57:42 
Today is 5 of October 2020, at 1.57 pm
    


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial guide, we will be studying about PHP and MySQL.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.