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:
Incredibly easy-to-use
Automatic handling of timezones
Ability to convert or verify anything that resembles a date no matter the data type
Can take any value that strtotime() can convert like "last Monday" or "+1 week 2 days 4 hours 2 seconds"
Output is always a built-in PHP DateTime class object
Easily add or subtract a period of time in years, months, days, hours, minutes or seconds to a given date
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
Optional $timezone: The default timezone as a string set at the top of the class file. By default this timezone is UTC time which is best for databases and companies in multiple timezones. A list of timezones can be found here on the PHP.net help pages.
Optional $forceFixDate: A boolean that forces fixing all date strings with dashes (default true, but note that this might be incompatible with some countries)
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$days: An integer specifying the number of days to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$hours: An integer specifying the number of hours to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$minutes: An integer specifying the number of minutes to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$months: An integer specifying the number of months to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$seconds: An integer specifying the number of seconds to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
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)
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$years: An integer specifying the number of years to increment. If the number is negative, the time interval will be subtracted instead and will return a result in the past.
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceDays($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceHours($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceMinutes($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceMonths($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceSeconds($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
Find the difference between two dates (that are converted if needed). The order of the dates doesn't matter.
Dates::differenceYears($date1, $date2)
$date1: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$date2: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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 outputprint_r($date);
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)
$date: A string containing a U.S. 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 stringechoDates::fixUSDateString('1-2-20')
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
$dob: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
If a date is specified, the last day of the month of the given date is returned.
Dates::getLastDayOfMonth($date = 'Today')
Optional $date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
Returns an integer containing the last day of the month.
$date: A date string (like '1-23-20' or 'January 23, 2020') or a date with time string (like '2020/01/23 13:23:45' or 'January 23, 2020 1:23pm'), a Unix timestamp integer, or PHP DateTime objects. This parameter can be any value that strtotime() can convert (like 'last Monday', or '+1 week 2 days 4 hours 2 seconds', or 'Now').
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.
Optional $day: An integer representing the day. If omitted, the current day is used.
Optional $month: An integer representing the month. If omitted, the current month is used.
Optional $year: An integer representing the year. If omitted, the current year is used.
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 resultprint_r($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.
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.