MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

Manipulation of dates and times in PHP with examples.

Here we are going to discuss about the PHP Date and times. how can we call current date and time in our code. There are nearly fifty date and time functions, so for this tutorial we will narrow them down to some of them which are very important to us.

FunctionDescription
date()Formats a Local Time/Date
time()Returns the Current Time as a Unix Timestamp
strtotime()Parses an English Textual Date or Time Into a Unix Timestamp
localtime()Get the local time
datesunrise()Returns time of sunrise for a given day and location
date_sunset()Returns time of sunset for a given day and location

PHP Date() Function

The date() function formats a timestamp so that it actually makes sense, such as 4:58 PM Thursday, June 18, 2020. The date() function accepts two arguments, according to the following syntax: date(format, timestamp).

Example Code :

Syntax

date(format, timestamp)

PHP provides over thirty-five case-sensitive characters that are used to format the date and time. These characters are:

 CharacterDescriptionExample
DayjDay of the Month, No Leading Zeros1 – 31
DaydDay of the Month, 2 Digits, Leading Zeros01 – 31
DayDDay of the Week, First 3 LettersMon – Sun
Dayl (lowercase ‘L’)Day of the WeekSunday – Saturday
DayNNumeric Day of the Week1 (Monday) – 7 (Sunday)
DaywNumeric Day of the Week0 (Sunday) – 6 (Saturday)
DaySEnglish Suffix For Day of the Monthst, nd, rd or th
DayzDay of the Year0 – 365
WeekWNumeric Week of the Year (Weeks Start on Mon.)1 – 52
MonthMTextual Representation of a Month, Three LettersJan – Dec
MonthFFull Textual Representation of a MonthJanuary – December
MonthmNumeric Month, With Leading Zeros01 – 12
MonthnNumeric Month, Without Leading Zeros1 – 12
MonthtNumber of Days in the Given Month28 – 31
YearLWhether It’s a Leap YearLeap Year: 1, Otherwise: 0
YearYNumeric Representation of a Year, 4 Digits1999, 2003, etc.
Yeary2 Digit Representation of a Year99, 03, etc.
TimeaLowercase Ante Meridiem & Post Meridiemam or pm
TimeAUppercase Ante Meridiem & Post MeridiemAM or PM
TimeBSwatch Internet Time000 – 999
Timeg12-Hour Format Without Leading Zeros1 – 12
TimeG24-Hour Format Without Leading Zeros0 – 23
Timeh12-Hour Format With Leading Zeros01 – 12
TimeH24-Hour Format With Leading Zeros00 – 23
TimeiMinutes With Leading Zeros00 – 59
TimesSeconds With Leading Zeros00 – 59
TimezoneeTimezone IdentifierExample: UTC, Atlantic
TimezoneI (capital i)Whether Date Is In Daylight Saving Time1 if DST, otherwise 0
TimezoneODifference to Greenwich Time In HoursExample: +0200
TimezonePDifference to Greenwich Time, With ColonExample: +02:00
TimezoneTTimezone AbbreviationExamples: EST, MDT …
TimezoneZTimezone Offset In Seconds-43200 through 50400

Using a combination of these characters and commas, periods, dashes, semicolons and backslashes, you can now format dates and times at which format you want.

PHP Time() Function

The time() function returns the current timestamp. The time() function can also return a modified timestamp. You can add or subtract any number of seconds to the function in order to return the timestamp of a previous or upcoming date and time.

Syntax

time()

For example, to return a timestamp for next week, or to return a timestamp from last week, I can add or subtract 7 days by determining how many seconds are involved (7 days * 24 hours per day * 60 minutes in an hour * 60 seconds in an hour = number of second in 7 days).

The result of above code will look like this:

Last Week: 1591871848
Next Week: 1593081448
Next Month: 1595068648

PHP Strtotime() Function

The strtotime() function accepts an English datetime description and turns it into a timestamp. It is a simple way to determine “next week” or “last monday” without using the time() function and a bunch of math.

Syntax

strtotime(time, now);

Some examples are:

PHP localtime() function

The localtime() function returns an array that contains the time components of a Unix timestamp.

Syntax

localtime(timestamp, is_assoc)

Example Code :

The result will be look like this:

Array ( [0] => 50 [1] => 2 [2] => 12 [3] => 18 [4] => 5 [5] => 120 [6] => 4 [7] => 169 [8] => 0 )

Array ( [tm_sec] => 50 [tm_min] => 2 [tm_hour] => 12 [tm_mday] => 18
[tm_mon] => 5 [tm_year] => 120 [tm_wday] => 4 [tm_yday] => 169 [tm_isdst] => 0 )

PHP datesunrise() Function

The date_sunrise() function returns the time of sunrise for a given day / location. 

Note : It returns the time of the sunrise, in the specified format, on success. FALSE on failure.

Syntax

date_sunrise(timestamp, format, latitude, longitude, zenith, gmtoffset)

Example Code :

the result will look like this:

PHP date_sunrise output for Lisbon, Portugal on Thu Jun 18 2020: sunrise 06:15, sunset 20:59
Date_sunrise Code Result

PHP date_sunset() Function

The date_sunset() function returns the time of sunset for a given day / location.

Syntax

date_sunset(timestamp, format, latitude, longitude, zenith, gmtoffset)

Example Code :

The Result will look like this:

PHP date_sunset output for Lisbon, Portugal on Thu Jun 18 2020 showing sunset time 20:59

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
MotoShare.in provides the perfect two-wheeler for every occasion—daily commuting, weekend escapes, tourist exploration, or test-riding your dream bike. Simplify your mobility with us!

Related Posts

What are the Authentication type in phpmyadmin which for config.inc.php

phpMyAdmin supports four authentication types, set per server with $cfg['Servers'][$i]['auth_type'] in config.inc.php: cookie (the default), config, http and signon. This page explains how each one works and…

Read More

What is opcache and Install / Configure in Linux

OPcache is PHP's built-in bytecode cache. It keeps compiled scripts in shared memory so PHP doesn't have to parse and compile the same files on every request….

Read More

PHP Code for Godaddy VPS Server SMTP Email Relay

This page shows how to send email from a GoDaddy VPS through GoDaddy's SMTP relay on port 25 with plain PHP sockets and no extra libraries. Below…

Read More

Laravel: oauth_access_tokens clean up process

using Laravel Passport—the oauth_access_tokens table balloons when tokens aren’t expiring, not being revoked on logout, or not being purged. Here’s a practical, safe cleanup + prevention plan…

Read More

Laravel: Complete Guide of Laravel Passport

Here’s a comprehensive, up-to-date step-by-step tutorial for setting up and using Laravel Passport (OAuth2 API authentication) with clear explanations, working code samples, and troubleshooting tips—suitable for local…

Read More

Laravel: What is APP_KEY in Laravel?

Let’s break down both what APP_KEY is in a Laravel .env file, why it matters, and its use cases. I’ll also explain how it’s generated and what…

Read More