sui_page Posted January 21, 2014 Share Posted January 21, 2014 Heya guys! I'm using an open source platform called Ubercart which works with PHP and I've implemented the following code. Essentially I'm working towards dynamically changing the price of product after a certain time. $dtA = new DateTime('01/15/2014 2:15PM'); if ( time() > $dtA ) { $item->price = $2.00; } else { $item->price = $item->price; } Problem is it doesn't work. In this instance I simply want the price of this item to be $2 if the current time has past '01/15/2014 2:15PM', which it has. Any guidance would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/ Share on other sites More sharing options...
Barand Posted January 21, 2014 Share Posted January 21, 2014 (edited) You are trying to compare a time value with DateTime object. Two options: $now = new DateTime(); if ($now > $dtA) { ... or if (time() > $dtA->getTimestamp()) { ... Edited January 21, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/#findComment-1465987 Share on other sites More sharing options...
sui_page Posted January 21, 2014 Author Share Posted January 21, 2014 You are trying to compare a time value with DateTime object. Two options: $now = new DateTime(); if ($now > $dtA) { ... or if (time() > $dtA->getTimestamp()) { ... I tried both these methods but couldn't get it to work. Would this be correct? $dtA = new DateTime('01/15/2014 2:15PM'); if (time() > $dtA->getTimestamp(){ $item->price = $item->price/2; } else { $item->price = $item->price; } Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/#findComment-1466050 Share on other sites More sharing options...
Barand Posted January 21, 2014 Share Posted January 21, 2014 Would this be correct? Depends on what you are trying to achieve. The else{} bit is totally redundant and as $dtA is set to a time in past then the current time will always be greater Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/#findComment-1466059 Share on other sites More sharing options...
sui_page Posted January 21, 2014 Author Share Posted January 21, 2014 (edited) Depends on what you are trying to achieve. The else{} bit is totally redundant and as $dtA is set to a time in past then the current time will always be greater Definitely the 'else'' is redundant. This is what I'm going for: $dtA = new DateTime('01/15/2014 2:15PM'); $dtb = new DateTime('01/15/2014 4:15PM'); if (time() > $dtA->getTimestamp(){ $ item->price = $item->price/2; } else if(time() > $dtB->getTimestamp(){ $item->price = $item->price/3; } So the price of this product will be half price for two hours and then be a third of the original price afterwards. Edited January 21, 2014 by sui_page Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/#findComment-1466063 Share on other sites More sharing options...
Barand Posted January 21, 2014 Share Posted January 21, 2014 (edited) The second bit will not execute if it is greater than the later time then it is greater than the earlier time. Test for the later time first. Edited January 21, 2014 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/285548-time-based-functions/#findComment-1466069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.