Get date plus or minus today

Several times I have came across same requirement of getting few days back from today, or few days after today. SugarCRM comes with few very handy date utility functions, and there is one which does the job!

global $timedate;
$today = $timedate->getInstance()->nowDbDate(); // Today
$earlier = $timedate->asDbDate($timedate->getNow()->modify("-30 days")); // 30 days before!
$later = $timedate->asDbDate($timedate->getNow()->modify("+2 months")); // 2 months later!

echo "Today:".$today; 
echo "<br />30 days before:".$earlier;
echo "<br />2 months later:".$later;

Simple! Isn’t it?

Take a look at include/TimeDate.php for many other suitable date time functions.

Note :

Follwing code returns only the date part pf current date
$today = $timedate->getInstance()->nowDbDate(); // Today – Output e.g. “2019-08-25”

If we want both date and time then simply call the nowDb as shown below:
$today = $timedate->getInstance()->nowDb(); // Current Date and Time – Output e.g. ” 2019-08-25 14:19:35″

Hope you find this blog post helpful.

Feel free to add comments and queries, that helps us to improve the quality of posts.

You can contact us at info@infotechbuddies.com

Thank you.

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *