Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views2 pages

Date and Time Function .

The document provides PHP code examples demonstrating various date functions such as date_diff(), checkdate(), modify(), and date_add(). It also includes a recursive function for calculating the factorial of a number. Each example is accompanied by its output to illustrate the functionality of the code.

Uploaded by

Koushik M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views2 pages

Date and Time Function .

The document provides PHP code examples demonstrating various date functions such as date_diff(), checkdate(), modify(), and date_add(). It also includes a recursive function for calculating the factorial of a number. Each example is accompanied by its output to illustrate the functionality of the code.

Uploaded by

Koushik M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. date_diff()-used to calculate the difference between two dates.

<?php
$date1=date_create('2023-12-01');
$date2=date_create('2024-01-01');
$diff=date_diff($date1,$date2);
echo $diff->format('%R%a days');
?>

output:
+31 days

2. Checkdate(month,day,year) -used to check if the provided date is valid date or not


<?php
if (checkdate(02,28,2025))
{
echo "valid date";
}
else
{
echo "invalid";

}
?>

output:
valid date

3.modify()

<?php
$date=date_create('2023-12-01');
date_modify($date,'+1 month');
echo date_format($date,'Y-m-d');
?>

output:
2024-01-01

4.date_add()-adds an amount of days,months,years,hours,minutes,and seconds to a dateTime


object.

<?php
$date=date_create('2023-12-01');
date_add($date,date_interval_create_from_date_string('10 days'));
echo date_format ($date,'Y-m-d');
?>

output:
2023-12-11

5.// helps to set the timezone to our local timezone

<?php
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s');
?>

output:
2025-04-16 11:13:30

//recursive
<?php
function factorial ($n)
{
if($n<=1)
{
return 1;
}
else
{
return $n*factorial($n-1);
}
}
echo factorial(5);
?>

You might also like