05
Oct

Code tip: Working with dates, sum 30 days, 10 working days ...

  • Font size: Larger Smaller
  • Hits: 6666
  • Print

When you build quote calculators you may need to show automatically some dates relative to the date the calculator is executed. We'll learn how to use the JFactory Date class in order to get calculated dates, for example adding 30 days to todays' date, or adding 10 business days.

You only need a single line to get the date which results adding 30 days to today, we'll also add a date format to show this result at the output:

$result= JFactory::getDate()->modify('+30 days')->format('d.m.Y');

JFactory::getDate() returns todays date, to that result we are adding 30 days, and then applying our preferred format. Now you can use ##result## at your output, pdf, emails or inline results to show the calculated date.

If we need to add only working days, thus excluding Saturdays and Sundays, we'd write at the code section:

$date = JFactory::getDate(); // We get today
$newDate = new JDate($date . ' +15 Weekday'); //We add 15 business days
$result= $newDate->format('Y-m-d'); //We format the result

This is the link to official Joomla information regarding this class. Here you will find some more examples and use cases to include at your calculators.

How to use JDate

Last modified on
Trackback URL for this blog entry.

Our clients' feedback