PHP Introduction | Text and Date Handling | Main Functions for Time
Next, let us discuss date and time. Time is handled quite differently inside a computer than in the ordinary world. In a computer, time is expressed as the amount of time that has elapsed from a certain reference date.
In PHP, date and time are represented by a value called a “timestamp.” This is the number of seconds that have elapsed since exactly midnight on January 1, 1970. This integer value is the most basic way to represent time.
However, people cannot immediately understand such a number, so PHP provides functions that obtain date and time as values such as year, month, day, hour, minute, and second based on that timestamp.
Getting the Current Timestamp
$variable = time();
This is the basic function for obtaining a timestamp. It gives you the timestamp value for the current moment.
Getting Date and Time Elements as an Array
$variable = getdate(timestamp);
This function is fundamental for using date and time. If you pass a timestamp as the argument, it returns each value such as year, month, day, hour, minute, and second as an associative array. If the argument is omitted, it returns the current time values. The main elements included in the returned associative array are as follows.
- year: year
- mon: month
- mday: day
- hours: hour
- minutes: minute
- seconds: second
- wday: day of week, an integer from 0 to 6
Getting a Timestamp from Date and Time Values
$variable = mktime(hour, minute, second, month, day, year);
This is the reverse operation. It checks the timestamp value for a time based on numbers for year, month, day, hour, minute, and second. There are six arguments, but actually only the first one is required. If the later arguments are omitted, the current date and time values are applied automatically.
Let us look at a simple example.
<?php
if ($_POST != null){
$num = $_POST['text1'] * 1;
$t = time() + (60 * 60 * 24 * $num);
$d = getdate($t);
$result = $num . " days from today is " .
$d['year'] . "-" . $d['mon'] . "-" .
$d['mday'] . ".";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<p><?php echo $result; ?></p>
<hr>
<form method="post" action="./index.php">
<input type="text" name="text1">
<input type="submit">
</form>
<hr>
</body>
</html>
In this example, when you enter a number of days in the input field and submit it, the program calculates and displays the date after that period from today.
First, it obtains the current timestamp with time, adds the number of seconds for the specified number of days (60 * 60 * 24 * number of days), and calculates the timestamp after the specified period. Finally, it obtains the year, month, and day from the timestamp with getdate. In the middle, the timestamp is just an integer, so the calculation is surprisingly simple.
Performing Date Calculations
Let us perform calculations using dates. Here, we will receive a date value and check how many days remain from today to that date.
First, look at the example code.
<?php
if ($_POST != null){
$str = $_POST['text1'];
$d = explode("/", $str);
$t1 = time();
$t2 = mktime(0, 0, 0, $d[1], $d[2], $d[0]);
$n = ceil(($t2 - $t1) / (60 * 60 * 24));
$result = $str . " is {$n} days from today.";
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<p><?php echo $result; ?></p>
<hr>
<form method="post" action="./index.php">
<input type="text" name="text1">
<input type="submit">
</form>
<hr>
</body>
</html>
If you enter a date such as 2020/1/13, separating year, month, and day with slashes (/), and submit it, the program calculates and displays the number of days from today to that date.
Previously, we calculated the date a certain number of days from today. Here, the basic idea is “convert to timestamps.” Convert the two dates to timestamps, calculate the difference, and divide by the number of seconds in one day to get the number of days.
$str = $_POST['text1'];
$d = explode("/", $str);
First, the submitted text is split by the slash (/) using explode, and the date is made into an array.
$t1 = time();
$t2 = mktime(0, 0, 0, $d[1], $d[2], $d[0]);
Next, prepare timestamps for the current date and the date from the prepared array values.
$n = ceil(($t2 - $t1) / (60 * 60 * 24));
Dividing the difference between the two timestamps by 60 * 60 * 24 gives the number of days. However, because hours, minutes, and seconds are not specified here, a fractional part will appear. To round it up, the ceil function is used. This function returns a value rounded up to the next integer.
In this way, date and time calculations become easy if you know how to convert back and forth between “year, month, day, hour, minute, second” values and timestamps.