PHP Date And Time Function

admin_img Posted By Bajarangi soft , Posted On 01-02-2024

The PHP date function is used to format a date or time into a human readable format. when time function allows you to set the default time zone from a PHP script.

Date And Time Function In PHP

1. date() function

The date() function is used to format the current date and time.

<?php
// Output: Current date in the format YYYY-MM-DD
echo date("Y-m-d");
?>
 

2. time()

The time() function returns the current Unix timestamp.

<?php
// Output: Current Unix timestamp
echo time();
?>
 

3. strtotime()

The strtotime() function parses a string containing a date and returns a Unix timestamp.

<?php
// Output: Unix timestamp for "2022-01-31"
echo strtotime("2022-01-31");
?>

4. date_create(), date_format()

These functions are used to create and format a date object.

<?php
// Output: Current date formatted as "d-m-Y"
$date = date_create();
echo date_format($date, "d-m-Y");
?>

5. strtotime() and date()

Combining strtotime() and date() to format a date from a string.

<?php
// Output: Formatted date from string
$dateString = "2022-01-31";
$timestamp = strtotime($dateString);
echo date("F j, Y", $timestamp);
?>

6. getdate()

The getdate() function returns an associative array containing information about the current date.

<?php
// Output: Associative array with date information
$dateInfo = getdate();
print_r($dateInfo);
?>

7. strftime()

The strftime() function formats a local time/date according to locale settings.

<?php
// Output: Formatted date according to locale
echo strftime("%A, %B %d, %Y");
?>

8. date_diff()

The date_diff() function calculates the difference between two dates.

<?php
// Output: Difference between two dates +731 days
$date1 = date_create("2022-01-31");
$date2 = date_create("2024-02-01");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
?>

These are just a few examples of PHP date and time functions. Depending on your needs, you may find other functions like strtotime(), date_add(), and date_sub() useful for more advanced date and time manipulation.

1. date() function

The date() function is used to format the current date and time.

<?php
// Output: Current date in the format YYYY-MM-DD
echo date("Y-m-d");
?>
 

2. time()

The time() function returns the current Unix timestamp.

<?php
// Output: Current Unix timestamp
echo time();
?>
 

3. strtotime()

The strtotime() function parses a string containing a date and returns a Unix timestamp.

<?php
// Output: Unix timestamp for "2022-01-31"
echo strtotime("2022-01-31");
?>

4. date_create(), date_format()

These functions are used to create and format a date object.

<?php
// Output: Current date formatted as "d-m-Y"
$date = date_create();
echo date_format($date, "d-m-Y");
?>

5. strtotime() and date()

Combining strtotime() and date() to format a date from a string.

<?php
// Output: Formatted date from string
$dateString = "2022-01-31";
$timestamp = strtotime($dateString);
echo date("F j, Y", $timestamp);
?>

6. getdate()

The getdate() function returns an associative array containing information about the current date.

<?php
// Output: Associative array with date information
$dateInfo = getdate();
print_r($dateInfo);
?>

7. strftime()

The strftime() function formats a local time/date according to locale settings.

<?php
// Output: Formatted date according to locale
echo strftime("%A, %B %d, %Y");
?>

8. date_diff()

The date_diff() function calculates the difference between two dates.

<?php
// Output: Difference between two dates +731 days
$date1 = date_create("2022-01-31");
$date2 = date_create("2024-02-01");
$diff = date_diff($date1, $date2);
echo $diff->format("%R%a days");
?>

These are just a few examples of PHP date and time functions. Depending on your needs, you may find other functions like strtotime(), date_add(), and date_sub() useful for more advanced date and time manipulation.

Related Post