Slide 1
Most trusted JOB oriented professional program
DevOps Certified Professional (DCP)

Take your first step into the world of DevOps with this course, which will help you to learn about the methodologies and tools used to develop, deploy, and operate high-quality software.

Slide 2
DevOps to DevSecOps – Learn the evolution
DevSecOps Certified Professional (DSOCP)

Learn to automate security into a fast-paced DevOps environment using various open-source tools and scripts.

Slide 2
Get certified in the new tech skill to rule the industry
Site Reliability Engineering (SRE) Certified Professional

A method of measuring and achieving reliability through engineering and operations work – developed by Google to manage services.

Slide 2
Master the art of DevOps
Master in DevOps Engineering (MDE)

Get enrolled for the most advanced and only course in the WORLD which can make you an expert and proficient Architect in DevOps, DevSecOps and Site Reliability Engineering (SRE) principles together.

Slide 2
Gain expertise and certified yourself
Azure DevOps Solutions Expert

Learn about the DevOps services available on Azure and how you can use them to make your workflow more efficient.

Slide 3
Learn and get certified
AWS Certified DevOps Professional

Learn about the DevOps services offered by AWS and how you can use them to make your workflow more efficient.

previous arrow
next arrow

Manipulation of dates and times in PHP with examples.

Spread the Knowledge

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:

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:

Chandan Kumar
Follow Me: