Calculate date differences & update status (PHP, Joomla)
In this article, we will learn: How to calculate date differences & update status according to result using PHP in Joomla.
1. We will use HTML5 geolocation API code(navigator.geolocation) that will prompt user to allow to know your location.
public function up_expired(){ $db = JFactory::getDbo(); $query=$db->getQuery(true); $input = JFactory::getApplication()->input; $query->select('id, created, expire_date'); $query->from('#__bt_properties'); $query="SELECT p.id, ev.value as expire_date, ev.extrafields_id, p.created, p.expire_date as expire_date_nouse FROM hy415_bt_properties as p INNER JOIN hy415_bt_property_extrafields_values as ev ON p.id=ev.property_id WHERE ev.extrafields_id=56;"; $db->setQuery($query); $list = $db->loadObjectList(); $today=date_create(date("Y-m-d H:i:s")); foreach($list as $item){ $created=$item->created.' 00:00:00'; $expire_date=$item->expire_date.' 11:00:00'; $diff=date_diff(date_create($expire_date), $today); $days_left=$diff->format("%R%a"); // +0 date last // -0 1 day left // -1 2 day left if($days_left>0) { $query="UPDATE #__bt_properties set published=0 WHERE id=$item->id"; $myfile = fopen("newfile.txt", "w") or die("Unable to open file!"); $txt = $query."\n"; fwrite($myfile, $txt); fclose($myfile); $db->setQuery($query); $result = $db->execute(); } else { } } exit; }
Code explanation:
This is function written inside controller of custom component. This is performing 3 tasks,
i. It will fetch expire date of properties, we want a property to be listed in frontend for a limited date of year.
ii. Next it will calculate expire date with today date. And depending on result, if it found that expired date has passed. Update published status of property in database, and set it to unpublished.
iii. Write log into text file on server.
This code is written for Joomla 3. And can be adapted into latest version as well with little modification
Still need help! no problem, feel free to contact us Today