Home Tutorial Php Phpdate PHP Date and Time Functions

 
 

Share on Google+Share on Google+
PHP Date and Time Functions
Posted on: August 30, 2009 at 12:00 AM
Advertisement
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.

 

Advertisement

Related Tags for PHP Date and Time Functions:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 30, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com