PHP Date and Time Functions


 

PHP Date and Time Functions

In this section we will discuss PHP Date and Time Functions with example code. The PHP Date and Time Functions is very useful in manipulating the date and time object in PHP program.

In this section we will discuss PHP Date and Time Functions with example code. The PHP Date and Time Functions is very useful in manipulating the date and time object in PHP program.

The PHP Date and Time Functions functions allows the developer to read the current system time in easy and convenient way. It also helps the developers to manipulate the date object in the PHP code.

Here is the some of the examples of PHP Date and Time Functions.

Printing amount of seconds passed since January 1 1970 00:00:00 GMT:

<?php print time(); ?>

Printing current date:

<?php print date("Y-m-d"); ?>

The output of the program is (at my system at the time of running the code): 2009-08-30 . In your case the above program will print the current date.

Checking the date validity:

You can use the following program to check the validity of a date:

<?php

//Validate Date
$month = 2;
$day = 2;
$year = 2009;
if (!$date = checkdate($month, $day, $year)) {
           print("Incorrect date.");
}
else {
          print("Date is valid.");
}
?>

<br>

<?php
//invalid Date
$month = 18;
$day = 36;
$year = 2009;
if (!$date = checkdate($month, $day, $year)) {
        
print("Incorrect date.");
}
else {
       
print("Date is invalid.");
}
?>

Read more Date Functions example.

 

Ads