0% found this document useful (0 votes)
8 views4 pages

WP - Date Function

Date functions in php

Uploaded by

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

WP - Date Function

Date functions in php

Uploaded by

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

<?

php

// Set the default timezone

Date_default_timezone_set(‘Asia/Kolkata’);

// Get the current date and time

Echo “Current Date and Time: “ . date(‘Y-m-d H:i:s’) . “\n”;

// Create a DateTime object

$date = date_create(‘2024-11-12’);

// Format the date

Echo “Formatted Date: “ . date_format($date, ‘l, F jS, Y’) . “\n”;

// Add 10 days to the date

Date_add($date, date_interval_create_from_date_string(’10 days’));

Echo “Date after adding 10 days: “ . date_format($date, ‘Y-m-d’) . “\n”;

// Subtract 5 days from the date

Date_sub($date, date_interval_create_from_date_string(‘5 days’));

Echo “Date after subtracting 5 days: “ . date_format($date, ‘Y-m-d’) . “\n”;

// Get the difference between two dates

$date1 = date_create(‘2024-11-12’);

$date2 = date_create(‘2024-12-25’);

$diff = date_diff($date1, $date2);

Echo “Difference between dates: “ . $diff->format(‘%R%a days’) . “\n”;

// Get the sunrise and sunset times

$sun_info = date_sun_info(time(), 12.9716, 77.5946);


Echo “Sunrise: “ . date(‘H:i:s’, $sun_info[‘sunrise’]) . “\n”;

Echo “Sunset: “ . date(‘H:i:s’, $sun_info[‘sunset’]) . “\n”;

// Get the current Unix timestamp

Echo “Current Unix Timestamp: “ . time() . “\n”;

// Convert Unix timestamp to date

$timestamp = 1731403200;

Echo “Date from Unix Timestamp: “ . date(‘Y-m-d H:i:s’, $timestamp) . “\n”;

?>

Explanation

Let’s go through each function and its parameters used in the PHP program:

Date_default_timezone_set

Purpose: Sets the default timezone used by all date/time functions in a script.

Parameters:

Timezone: A string representing the timezone identifier (e.g., ‘Asia/Kolkata’).

Date

Purpose: Formats a local date and time.

Parameters:

Format: A string specifying the format of the outputted date string (e.g., ‘Y-m-d H:i:s’).

Timestamp (optional): An integer Unix timestamp. If omitted, the current time is used.

Date_create

Purpose: Creates a new DateTime object.

Parameters:

Time (optional): A string representing a date/time (e.g., ‘2024-11-12’). If omitted, the current date and
time are used.
Date_format

Purpose: Returns a formatted date string from a DateTime object.

Parameters:

Object: A DateTime object.

Format: A string specifying the format of the outputted date string (e.g., ‘l, F jS, Y’).

Date_add

Purpose: Adds an interval to a DateTime object.

Parameters:

Object: A DateTime object.

Interval: A DateInterval object representing the interval to add (e.g.,


date_interval_create_from_date_string(’10 days’)).

Date_interval_create_from_date_string

Purpose: Creates a DateInterval object from a relative parts string.

Parameters:

Time: A string representing the interval (e.g., ’10 days’).

Date_sub

Purpose: Subtracts an interval from a DateTime object.

Parameters:

Object: A DateTime object.

Interval: A DateInterval object representing the interval to subtract (e.g.,


date_interval_create_from_date_string(‘5 days’)).

Date_diff

Purpose: Returns the difference between two DateTime objects.

Parameters:

Datetime1: A DateTime object representing the first date.

Datetime2: A DateTime object representing the second date.

Absolute (optional): A boolean indicating whether to return an absolute difference. Default is FALSE.

Date_sun_info

Purpose: Returns an array with information about sunset/sunrise and twilight begin/end.
Parameters:

Time: An integer Unix timestamp.

Latitude: A float representing the latitude of the location.

Longitude: A float representing the longitude of the location.

Time

Purpose: Returns the current Unix timestamp.

Parameters: None.

Date (used again for converting Unix timestamp)

Purpose: Formats a local date and time.

Parameters:

Format: A string specifying the format of the outputted date string (e.g., ‘Y-m-d H:i:s’).

Timestamp: An integer Unix timestamp to format.

You might also like