phppup Posted January 30, 2019 Share Posted January 30, 2019 At this point I clearly have too much time on my hands, as I started to tinker with different DATE options. $originalDate = 2003-03-03; //March 3rd, 2003 is the day number 62 in that year $day_of_the_YEAR = date("z", strtotime($originalDate)); //provides the day of the year starting with ZERO on 01/01 echo "($day_of_the_YEAR + 1)"; //allows me to ADD the ONE to compensate for the ZERO that begins the counter This is all OK, but then I tried to incorporate the English ordinal suffix by using the date format mechanism S. Not only did it not work, but when I edited my code to: $day_of_the_YEAR = date("zS", strtotime($originalDate)); echo "($day_of_the_YEAR)"; //because it won't work at all with the PLUS 1 the result for echo $day_of_the_YEAR was 61rd [which is neither accurate for March 3 nor acceptable as the correct English ordinal suffix. Is there a remedy, or is this due to trying to combine a non-date with the S switch? Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/ Share on other sites More sharing options...
Barand Posted January 30, 2019 Share Posted January 30, 2019 if it helps, here's a function what I wrote several years ago /******************************************** * Derive ordinal suffix for $n * * @param int $n the number * @returns string number with suffix eg 23rd */ function ordSuffix($n) { $str = "$n"; $t = $n > 9 ? substr($str,-2,1) : 0; $u = substr($str,-1); if ($t==1) return $str . 'th'; else switch ($u) { case 1: return $str . 'st'; case 2: return $str . 'nd'; case 3: return $str . 'rd'; default: return $str . 'th'; } } EG echo ordSuffix(61) //--> 61st Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564095 Share on other sites More sharing options...
phppup Posted January 30, 2019 Author Share Posted January 30, 2019 I may implement it. But I thank you either way. Just fiddling around, so it's not a major component of anything I am working with. And I'm actually happy to see that my own thought process to develop my own script to overcome this was along similar lines. Although yours is much, much cleaner and concise. Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564096 Share on other sites More sharing options...
phppup Posted January 31, 2019 Author Share Posted January 31, 2019 On a related note, I now have the basics to send a message to people listed in a database on their birthdate. It has occurred to me: what is the best way to deal with people with February 29 as their birthday? While the most simple solution would be to just forget them, that seems unfair. LoL Would the more practical approach be coding along the lines of: if(today +1) = March 1, then also include 2/29 from birthdate column? Will something like that work? Is it practical? Is there a better way? Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564100 Share on other sites More sharing options...
Barand Posted January 31, 2019 Share Posted January 31, 2019 You would do that only if it's not a leap year otherwise you send 2 birthday messages. send message WHERE RIGHT(dob, 5) = RIGHT(CURDATE(), 5) OR (YEAR(CURDATE())%4 <> 0 AND RIGHT(dob, 5) = '02-29' AND RIGHT(CURDATE(), 5) = '03-01') Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564109 Share on other sites More sharing options...
cyberRobot Posted January 31, 2019 Share Posted January 31, 2019 14 hours ago, phppup said: This is all OK, but then I tried to incorporate the English ordinal suffix by using the date format mechanism S. When displaying dates like March 3, 2003, you shouldn't use an ordinal number. More information can be found here:https://www.quickanddirtytips.com/education/grammar/writing-dates Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564110 Share on other sites More sharing options...
phppup Posted January 31, 2019 Author Share Posted January 31, 2019 Barand, will that work in PHP or is that SQL only? If only this were a leap year I could test it in real time. LOL Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564114 Share on other sites More sharing options...
Barand Posted January 31, 2019 Share Posted January 31, 2019 (edited) It is SQL. I was under the impression your data was in a DB table. You could test it by substituting '2020-02-29' for CURDATE() while you test Edited January 31, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564115 Share on other sites More sharing options...
phppup Posted January 31, 2019 Author Share Posted January 31, 2019 Okay, got it. Yes, dates are in table. I believe I used SQL previously and this will add to it. If I'm not mistaken, I also used a PHP adaptation to test outside of the table (so that I could get comfortable with it and see results more easily). Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564122 Share on other sites More sharing options...
Zane Posted February 1, 2019 Share Posted February 1, 2019 Although the manual doesn't explicitly state it, you're not going to be able to use the "S" suffix successfully with "z" because "z" can return a value of 0. PHP's date function is going to return 0th day for a Jan 1st date. You'll need to follow Barand's lead here and use a custom function if you want the e.g 53rd day of the year. The manual also states that this "S" suffix is meant for the ordinal suffix for the date of the month, not the year. Quote English ordinal suffix for the day of the month, 2 characters http://php.net/manual/en/function.date.php So, something like this $a = "2012-01-01"; echo date("zS", strtotime($a)); echo "<br />"; $a = "2012-01-02"; echo date("zS", strtotime($a)); echo "<br />"; $a = "2015-01-03"; echo date("zS", strtotime($a)); echo "<br />"; will return this: Quote 0st1nd2rd Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564126 Share on other sites More sharing options...
Barand Posted February 1, 2019 Share Posted February 1, 2019 9 hours ago, phppup said: and see results more easily It is easy to see SQL results. Just run the query in your database GUI (PhpMyAdmin, Workbench etc) or run it from the command line EG mysql> SELECT dob -> , fname -> , lname -> , CASE WHEN RIGHT(dob, 5) = RIGHT(CURDATE(), 5) THEN 'Happy Birthday' -> ELSE '' -> END as message -> FROM pupil; +------------+----------+------------+----------------+ | dob | fname | lname | message | +------------+----------+------------+----------------+ | 2001-06-22 | Adam | Simms | | | 2001-03-04 | Allan | Blair | | | 2002-02-01 | Anna | Hamilton | Happy Birthday | | 2001-08-02 | Anne | Bailey | | | 2001-10-04 | Anthony | Bell | | | 2000-12-13 | Caroline | Freeman | | Quote Link to comment https://forums.phpfreaks.com/topic/308248-a-silly-date-question/#findComment-1564128 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.