Easy PHP Date Class for PHP

This date utility class is static (you do not need to create any type of object to use it) for ease of use, performance, and convenient code completion. All of the code is in a single file to make it incredibly easy to install and learn.

I also made every effort to take care of all the details like:
Tip Some basic knowledge of how the built in PHP DateTime work is helpful but not necessary.

Table of Contents

convertToDate

Converts any English textual datetimes into a DateTime object.

Can convert any data type into DateTime objects. It can also receive DateTime objects as a parameter but will simply return them.

Dates::convertToDate($date, $timezone = self::DEFAULT_TIMEZONE, $forceFixDate = true)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Convert a string into a date $string = 'Jan 23 2020'; $date = Dates::convertToDate($string); // View the output print_r($date);
Back to top

addDays

Add or subtract a specified number of days to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of days into the future or past.

Dates::addDays($date, $days)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add days to a date $date = Dates::addDays('2020/01/23', 7); // View the output print_r($date);
Back to top

addHours

Add or subtract a specified number of hours to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of hours into the future or past.

Dates::addHours($date, $hours)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add hours to a date $date = Dates::addHours('2020/01/23 04:56', 12); // View the output print_r($date);
Back to top

addMinutes

Add or subtract a specified number of minutes to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of minutes into the future or past.

Dates::addMinutes($date, $minutes)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add minutes to a date $date = Dates::addMinutes('2020/01/23 04:56', 30); // View the output print_r($date);
Back to top

addMonths

Add or subtract a specified number of months to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of months into the future or past.

Dates::addMonths($date, $months)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add months to a date $date = Dates::addMonths('2020/01/23', 6); // View the output print_r($date);
Back to top

addSeconds

Add or subtract a specified number of seconds to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of seconds into the future or past.

Dates::addSeconds($date, $seconds)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add seconds to a date $date = Dates::addSeconds('2020/01/23 04:56', 10); // View the output print_r($date);
Back to top

addYears

Add or subtract a specified number of years to a date.

This method can take and convert any type of date and return a DateTime object with a specified number of years into the future or past.

Dates::addYears($date, $years)
Returns a DateTime object. If the date parameter cannot be converted, false is returned.
// Add years to a date $date = Dates::addYears('2020/01/23', -2); // View the output print_r($date);
Back to top

differenceDays

Get the number of days between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceDays($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in days between two dates $date1 = 'January 23, 2020'; $date2 = 'Today'; $value = Dates::differenceDays($date1, $date2); // View the output print_r($date);
Back to top

differenceHours

Get the number of hours between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceHours($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in hours between two dates $date1 = 'January 23, 2020 4:00am'; $date2 = 'January 23, 2020 5:00pm'; $value = Dates::differenceHours($date1, $date2); // View the output print_r($date);
Back to top

differenceMinutes

Get the number of minutes between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceMinutes($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in minutes between two dates $date1 = 'January 23, 2020 4:00'; $date2 = 'January 23, 2020 4:56'; $value = Dates::differenceMinutes($date1, $date2); // View the output print_r($date);
Back to top

differenceMonths

Get the number of months between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceMonths($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in months between two dates $date1 = 'January 1, 2020'; $date2 = 'July 1, 2020'; $value = Dates::differenceMonths($date1, $date2); // View the output print_r($date);
Back to top

differenceSeconds

Get the number of seconds between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceSeconds($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in seconds between two dates $date1 = 'January 23, 2020 4:00:00'; $date2 = 'January 23, 2020 4:00:30'; $value = Dates::differenceSeconds($date1, $date2); // View the output print_r($date);
Back to top

differenceYears

Get the number of years between two dates.

Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.

Dates::differenceYears($date1, $date2)
Returns a DateTime object. If either of the date parameters cannot be converted into a date, false is returned.
// Find the difference in years between two dates $date1 = 'January 1, 2019'; $date2 = 'January 1, 2021'; $value = Dates::differenceYears($date1, $date2); // View the output print_r($date);
Back to top

fixUSDateString

U.S. dates with dashes do not always convert correctly in PHP so replace them with foward slashes.

In PHP, if the separator is a slash (/), then the American m/d/Y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-Y format is assumed. For more information, see this link: StackOverflow: Wrong date while converting date in PHP.

Dates::fixUSDateString($date)
If the passed in value is a string, returns the fixed date string, otherwise returns the value passed in to the function.
// Show a fixed U.S. date string echo Dates::fixUSDateString('1-2-20')
Back to top

formatDate

Formats a date.

Formats any type of date.

Dates::formatDate($date, $format, $timezone = self::DEFAULT_TIMEZONE)
Returns a string containing the formatted date. If the date parameter cannot be converted, false is returned.
// Format a date for MySQL $date = 'January 2, 2020 3:45pm'; echo Dates::formatDate($date, 'Y-m-d H:i:s');
Back to top

getAge

Returns the age of someone or something calculated from a date.

Dates::getAge($dob, $timezone = self::DEFAULT_TIMEZONE)
Returns the age as an integer.
echo Dates::getAge('2003-04-05');
Back to top

getCurrentYear

Returns the current year.

Dates::getCurrentYear($timezone = self::DEFAULT_TIMEZONE)
Returns the current years as an integer.
echo Dates::getCurrentYear();
Back to top

getLastDayOfMonth

Returns the last day of the month.

If a date is specified, the last day of the month of the given date is returned.

Dates::getLastDayOfMonth($date = 'Today')
Returns an integer containing the last day of the month.
echo Dates::getLastDayOfMonth('February 2021');
Back to top

getTimeZonesInCountry

Gets a list of timezones for the specified country.

Dates::getTimeZonesInCountry($country)
Returns a numerically indexed array containing all defined timezone identifiers.
print_r(Dates::getTimeZonesInCountry('US'));
Back to top

isDate

Determines if a variable contains a valid date.

This method is useful for validating user input or making sure a leap year is enforced.

Dates::isDate($value, $timezone = self::DEFAULT_TIMEZONE)
If a value can be converted into a date successfully, true is returned, otherwise false is returned.
// Determine if a date is valid $date = 'January 2, 2020 3:45pm'; if (Dates::isDate($date)) { echo 'Valid date'; } else { echo 'Invalid date'; }
Back to top

isTimeZoneInCountry

Determines if a time zone is in the specified country.

The search is not case sensitive.

Dates::isTimeZoneInCountry($timezone, $country)
If the timezone is in the specified country, true is returned, otherwise false is returned.
if (Dates::isTimeZoneInCountry('America/Chicago', 'US')) { echo 'Yes'; } else { echo 'No'; }
Back to top

Builds a date from date parts

Creates a DateTime object from combining different parts of a date.

This method is helpful combining parts of a web form into a full date. And part of a date is optional, and any part of the date omitted (false) will use parts of today's date.

Dates::makeDate($day = false, $month = false, $year = false)
A PHP DateTime class object is returned.
// Set our date parts $day = 15; $month = 6; $year = 1999; // Combine the parts $date = Dates::makeDate($day, $month, $year); // Show the result print_r($date);
Back to top

now

Returns the currect date and time.

The difference between the now() and today() methods is the now() method returns both the current date plus the time, while the today() method only returns the date without the time.

Dates::now($format = false, $timezone = self::DEFAULT_TIMEZONE)
If the format parameter is specified, a string with the formatted date returned. If there is format parameter, a PHP DateTime object is returned.
// Get the current date and time $now = Dates::now(); // Show the result print_r($now);
Back to top

today

Returns the currect date.

The difference between the now() and today() methods is the now() method returns both the current date plus the time, while the today() method only returns the date without the time.

Dates::today($format = false, $timezone = self::DEFAULT_TIMEZONE)
If the format parameter is specified, a string with the formatted date returned. If there is format parameter, a PHP DateTime object is returned.
// Get the current date $today = Dates::today(); // Show the result print_r($today);
Back to top