Create a Date With PHP mktime()
The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used (as shown in the examples above).
The mktime() function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.
Syntax : mktime(hour,minute,second,month,day,year)
The example below creates a date and time from a number of parameters in the mktime() function:
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
<html>
<body>
<?php
$d=mktime(11, 14, 54, 8, 12, 2014);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
Output :
Created date is 2014-08-12 11:14:54am
Create a Date From a String With PHP strtotime()
The PHP strtotime() function is used to convert a human readable string to a Unix time.
Syntax : strtotime(time,now)
The example below creates a date and time from the strtotime() function:
Example :
<!DOCTYPE html>
<html>
<body>
<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
<html>
<body>
<?php
$d=strtotime("10:30pm April 15 2014");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>
</body>
</html>
Output :
Created date is 2014-04-15 10:30:00pm
PHP is quite clever about converting a string to a date, so you can put in various values:
<!DOCTYPE html>
<html>
<body>
<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>
</body>
</html>
<html>
<body>
<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>
</body>
</html>
Output :
2014-05-12 12:00:00am
2014-05-17 12:00:00am
2014-08-11 09:56:43pm
2014-05-17 12:00:00am
2014-08-11 09:56:43pm
However, strtotime() is not perfect, so remember to check the strings you put in there.
More Date Examples
The example below outputs the dates for the next six Saturdays:
<!DOCTYPE html>
<html>
<body>
<?php
$startdate=strtotime("Saturday");
$enddate=strtotime("+6 weeks",$startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate),"<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
</body>
</html>
<html>
<body>
<?php
$startdate=strtotime("Saturday");
$enddate=strtotime("+6 weeks",$startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate),"<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
</body>
</html>
Output :
May 17
May 24
May 31
Jun 07
Jun 14
Jun 21
May 24
May 31
Jun 07
Jun 14
Jun 21
The example below outputs the number of days until 4th of July:
<!DOCTYPE html>
<html>
<body>
<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>
</body>
</html>
<html>
<body>
<?php
$d1=strtotime("July 04");
$d2=ceil(($d1-time())/60/60/24);
echo "There are " . $d2 ." days until 4th of July.";
?>
</body>
</html>
Output
There are 54 days until 4th of July.

0 Response to "Create a Date With PHP"
Posting Komentar