Google News
logo
PHP Program to Create a date and time from a number of parameters in mktime()
In the following example of PHP program to create a date and time from a number of parameters using the `mktime()` function :
Program :
<?php
// create a date and time from a number of parameters
$hour = 12;
$minute = 30;
$second = 45;
$month = 5;
$day = 1;
$year = 2023;

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

// output the timestamp
echo "Timestamp: $timestamp<br>";

// convert timestamp to a date string
$date = date('Y-m-d H:i:s', $timestamp);

// output the date string
echo "Date: $date";
?>
Output :
Timestamp: 1682944245
Date: 2023-05-01 12:30:45
In this program, we first set the variables for the hour, minute, second, month, day, and year that we want to create a date and time from. Then, we use the `mktime()` function to create a timestamp from these parameters.

We output the timestamp using the `echo` statement. Then, we use the `date()` function to convert the timestamp to a human-readable date string. We format the date string to display the year, month, day, hour, minute, and second, separated by hyphens and colons, using the format `'Y-m-d H:i:s'`.

Finally, we output the date string using the `echo` statement.