How To Handle Date and Time in dynamic website with PHP Functions
What Will I Learn?
- We will learn how to use PHP functions handle date and time.
- We will learn about function date (), mktime () and strtotime ().
- We will learn how to make scheduling (scheduler).
Requirements
- You have basic about PHP.
- You have basic how to use variable in php.
- You should also have a text editor software.
Difficulty
- Intermediate
Tutorial Contents
Some PHP functions to handle date and time include, date (), mktime () and strtotime () functions. These functions are also used when making scheduling as well as tool maintenance controls or just making a notice of the birthday date of someone we want.
In dynamic website creation, the date role serves to mark the post time of an article, keep track of the number of visits per day and many other functions. Well I will explain each function date (), mktime () and strtotime ().
1. The date () function
This function is to display the current server time and this function takes time from the server and displays it according to the format requested by the script.
The required format parameter in the date() function specifies how to format the date/time.
- d -> Represents the day of the month (01 to 31)
- m -> Represents a month (01 to 12)
- y -> represents a year (in four digits)
Other characters,like "/", ".", or "_" can also be inserted between the letters to add additional formatting.
How to write date () function:
date(format) or date(format,timestamp) An example of using the date () function :
<?php
echo date("Y-m-d")."<br>"; //Example Showing 2018-01-31
echo date("h:i:sa"); //Example Showing 10:30:50am
?>
2. Mktime () function
The optional timestamp parameter in the date() function spesifies a timestamp. if you do not specify a timestamp, the current date and time will be used.
The Mktime() function return the unix timestamp for a date. This function is used to create new time with timestamp in standard time format. For example, want to make timestamp five days after today or three months ago.
The unix timestamp contains the number of seconds between the unix epoch (January 1 1970 00:00:00 GMT) and the time specified.
How to write Mktime () function :
mktime(hour,minute,second,month,day,year) An example of using the Mktime () function :
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo date("Y-m-d h:i:s a", $d); // Showed 2014-08-12 11:14:54 am
?>
3. Strtotime () function
This function is used to convert string to Unix timestamp.
How to write Strtotime () function :
strtotime(string) An example of using the Strtotime () function :
<?php
$d=strtotime("10:30pm April 15 2014");
echo date("Y-m-d h:i:s a", $d); //Showed 2014-04-15 10:30:00 pm
?>
Curriculum
Posted on Utopian.io - Rewarding Open Source Contributors
@bambangpratama, No matter approved or not, I upvote and support you.
Your contribution cannot be approved because it does not follow the Utopian Rules, and is considered as plagiarism. Plagiarism is not allowed on Utopian, and posts that engage in plagiarism will be flagged and hidden forever.
You plagiarised from https://www.w3schools.com/php/php_date.asp.
You can contact us on Discord.
[utopian-moderator]