Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/308248-a-silly-date-question/
Share on other sites

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

 

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.

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?

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')

 

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

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

0st
1nd
2rd

 

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    |                |

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.