Jump to content

bg17aw

Members
  • Posts

    13
  • Joined

  • Last visited

bg17aw's Achievements

Member

Member (2/5)

0

Reputation

  1. You're very wise to avoid cheap jokes. Not wise enough to avoid wasting my time and yours. Surely you do realize you posted about 5 times in this topic without any contribution. Just innuendos and cheap jokes you don't make but mention anyway i couldn't take a hint or anything else useful from your posts, no offence. Thanks a lot to Barand who gave it a shot. I also took the time to post my solution. Hope you find some better use of your time. I surely didn't spend two days working on a trivial interval search I could have just copy pasted Barand solution or the Stackoverflow ones, but I actually came up with my own and pretty happy with it. The data structure is good enough, to save in the DB and retrieve easily. As you can see in my solution above, I do NOT have to change the date format as you kept on saying for some reason. Peace! PS: the "I know there is a better way to do it but not telling" attitude childish and froned upon on all forums. This will not bring a lot of new members in here. I probably avoid this community because of people like you
  2. I'll probably go with this solution, this would eliminate the need to sort the array: function get_earlybooking_percentage($earlybooking) { $today = new DateTime(); $matching = array(); foreach ($earlybooking as $item) { if ($today < DateTime::createFromFormat('d-m-Y', $item['beforedate'])) { $matching[] = $item['percentage']; } } return max($matching); }
  3. Thanks. That's because you are using string comparison, I want to avoid any solution not involving the use of DateTime objects
  4. Dude, the date format is irrelevant, I already mentioned I want to use DateTime objects and provided a link with the code to sort using those. If you're using DateTime objects, the format is irrelevant. Barand solution is not the solution I'll be using, I was only pointing out the issue was HOW to get the percentage when the condition is met more than once. I was not asking how I can sort the array, but if I need to, is there another way. Barand is doing string comparison, which of course is totally wrong when dealing with dates, I would advise to always stick to DateTime objects when dealing with datetime in PHP. Again, I already indicated a solution of sorting using DateTime objects: usort($array, function($a, $b) { return $a['date']->format('U') - $b['date']->format('U'); }); Or, another example: usort($arr, function($a, $b) { $ad = new DateTime($a['dateTime']); $bd = new DateTime($b['dateTime']); if ($ad == $bd) { return 0; } return $ad < $bd ? -1 : 1; }); You could also use this if you're worried about format: foreach ($data as $rec) { if ($today <= DateTime::createFromFormat('d-m-Y', $rec['beforedate'])) { $discount = $rec['percentage']; break; } } Take it easy, Just because Barand didn't provide a solution using DateTime objects it doesn't mean it would never work. I certainly do not want strcomp or strtotime
  5. There is absolutely no confusion, and definetely not regarding the date format, you are missing the point here. The only issue is how to get the percentage taking into account that current date will be "before" more than one of the "beforedates". That's it. Please see Barand answer, he got it. Don't assume you know more than the person who asked the question.
  6. No, that's not an issue, there is actually a link in my code to an example on how to sort array of DateTime objects on date. The real question is how do I get the discount percentage I need, the only issue being a date (the booking date, which would be today) can be "beforedate" one, two or more dates. That is the only issue, sorry if it is not clear enough, I am not an English native and so on. So I can have a function like this: foreach ($earlybooking as $key => $val) { if ( new DateTime($val['beforedate']) >= $today) return $val['percentage']; } but would not be enough, because for example for today 03-03-2017, there are two possible answers.
  7. No, that not an issue when you are dealing with DateTime objects. Thanks for your input anyway
  8. I want to offer a discount based on early booking but not sure how to get the discount that applies based on the current date. I have seen some usort examples but not very familiar to me. I want to keep using DateTime objects (not timestamps). $today = new DateTime(); $data['earlybooking'] = array( array('percentage' => '25', 'beforedate' => '10-12-2016' ), array('percentage' => '15', 'beforedate' => '31-03-2017' ), array('percentage' => '10', 'beforedate' => '30-04-2017' ), array('percentage' => '20', 'beforedate' => '31-01-2017' ) ); // foreach ? Or maybe usort? // usort example: http://stackoverflow.com/questions/7127764/sort-array-of-objects-by-date-field Any ideas? Thanks
  9. Hi, I am wondering if anyone else can confirm this error, and also can comment on whether it should be reported and how: - page 41, Constant Properties chapter: Constant properties can contain only primitive values. You cannot assign an object to a constant. But objects are also primitive values, as shown here: http://php.net/manual/en/language.types.intro.php Any thoughts?
  10. I am using WAMP (Win 7, PHP 5.4) I worked on a project using session_start() about 3 months ago. Now 3 months later I created a new php file, and wrote: session_start(); echo "<pre>"; print_r($_SESSION); echo "</pre>"; This displays the content of the session from 3 months ago. Anyone knows why this happens? Is it normal? Thank you!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.