Time Management

PHP

Time and Date are two important factors in each and every web application.

Timestamp

PHP timestamp is calculated as UNIX timestamp and are measured in the number of seconds since the Unix Epoch that is,January 1 1970 00:00:00 Greenwich Mean Time (GMT). A timestamp which contains sequence of characters. Timestamp numeric value returned by the time function depends on the default time zone. date_default_timezone_set() can used to set the default time zone.

In php current Timestamp can be displayed by using time() function,

<?php echo time(); ?>

Result:

 1510004781

PHP Date() Function

PHP Date() function converts timestamp value to human readable format.

Syntax:

date(format,timestamp)

format: required.Which specifies the format to display our output.
timestamp: optional. If timestamp is not provided, then the output become current date and time of the format we provided.
For Example print current date,month and year.

<?php echo date('d-m-Y'); ?>

Result:

 21-11-2017

‘d-m-Y’ – Which is the format we specified to print the result.
d-> print current date of the timestamp provided or current day
m-> print current month of the timestamp provided or current day
Y-> print current year of the timestamp provided or current day.

Another Example to print current time,minute and second.

<?php echo date('h:i:s'); ?>

Result is:10:10:20

‘h:i:s’ – Which is the format we specified to print the result.
h-> print current hour of the  current timestamp
i-> print current minute of the current timestamp
s-> print current second of the current timestamp

some format parameters used in date() are:

Day d – Numeric representation of day leading zeros(01 to 31)D-Textual representation of day by first 3 letters(mon)

j – Numeric representation of day without leading zeros(1 to 31)

l – Full extual representation of the day(monday)

Month m – Numeric representation of month leading zeros(01 to 12)

M – Textual representation of a month by three letters(jan)

n – Numeric representation of a month without leading zeros(1 to 12

t – Total number of days in the given month

Year Y – Four digit representation of a year

y -Two digit representation of a year

meridiem a – Lowercase am or pm

A – Uppercase AM or PM

Time g – 12-hour format of an hour(1 to 12)

G – 24-hour format of an hour(0 to 23)

h – 12-hour format of an hour(01 to 12)

H – 24-hour format of an hour(00 to 23)

i – Minutes with leading zeros(00 to 59)

s – Seconds, with leading zeros(00 to 59)

Unix Epoch U – The seconds since the Unix Epoch(January 1 1970 00:00:00 GMT)

PHP mktime() function

In php we can make timestamp for a date by using mktime() function. The resultant timestamp is in Unix format.

Syntax:
mktime(hour,minute,second,month,day,year);

the function contains a number of parameters and are,hour – specifies hour of the dayminute – specifies minute of the daysecond – specifies second of the daymonth -specifies month of the dayday – specifies day of the yearyear – specifies year of the dayExample is,

<?php echo mktime(10,10,0,11,21,2017); ?>

Result is:

 1511259000

Another example,which shows how to make time using date function,

<?php $d = mktime(date('h'),date('i'),date('s'),date('m'),date('d'),date('Y'));

echo $d;?>

Result:

1511259000

The above example results the timestamp of current hour,minute,second,month,day and year.

PHP strtitime() function

PHP strtitile() function is converting the english date string to unix timestamp.syntax:
strtotime(string,now);string :Required parameter which is the English string.now: optionalFor example

<?php $dt = strtotime('November 21 2017');
echo date('d-m-Y',$dt);
?>

Result is:

21-11-2017

PHP getdate() Function

PHP getdate() function returns an associative array, contains the information of date and time for a unix timestamp.Associative array contains following informations:-

  • [seconds] – seconds
  • [minutes] – minutes
  • [hours] – hours
  • [mday] – day of the month
  • [wday] – day of the week
  • [mon] – month
  • [year] – year
  • [yday] – day of the year
  • [weekday] – name of the weekday
  • [month] – name of the month
  • [0] – timestamp

synatx:

getdate(timestamp);

timestamp : optional
for example

<?php print_r(getdate()); ?>

Result:

Array ( [seconds] => 29 [minutes] => 10 [hours] => 10 [mday] => 21 [wday] => 
3 [mon] => 11 [year] => 2017 [yday] => 324 [weekday] => Tuesday [month] => 
November [0] =>1511259029 )

Leave a Reply

Your email address will not be published. Required fields are marked *