-1

Using PHP getdate(); I get the following array output:

Array (
    [seconds] => 54 
    [minutes] => 26 
    [hours] => 19 
    [mday] => 10 
    [wday] => 5 
    [mon] => 12 
    [year] => 2021 
    [yday] => 343 
    [weekday] => Friday 
    [month] => December 
    [0] => 1639164414
)

I want to use if the value [yday] equals a particular number then echo message but unsure how this can be done. The idea is to have a different message echo each day - I know how to do this in JavaScript but been tasked with doing it in PHP which is something I'm still learning.

For example if yday = 100 echo message, if yday = 200 echo different message.

So, if there is 365 days in a year I simply want to output (echo) a different message every day of the year based on the yday number. It doesn't need to be an exact time/zone, the message must stay static and we don't want it to be random, or use cookies with random to store it for 24 hours.

There might even be a better way of doing this without yday, any help appreciated.

Simon Hayter
  • 3,131
  • 27
  • 53
  • 1
    What have you tried already, where are you stuck? Create an array of messages for each day, show that message accordingly – brombeer Dec 10 '21 at 19:56
  • 1
    This is pretty simple to accomplish with PHP. You can create an array of messages using the day number as the key, and then use `isset($messages[$day])` to see if that days message exists, if so print it. – Jacob Mulquin Dec 10 '21 at 20:00

1 Answers1

1

Create an array of messages you want to show for each day, with the day number as key. Then print the message for that day:

<?php

$messages = [
    0 => 'foo bar',
    1 => 'hello world',
    100 => 'message',
    200 => 'different message',
    343 => 'lorem ipsum',
];

$today = getdate();
$yday = $today['yday'];
echo $messages[$yday];
brombeer
  • 8,716
  • 5
  • 21
  • 27
  • Awesome exactly what I was looking, I had tried similar but got confused, I was using `==` and `{ }` instead of the array brackets in my varible. Kudos, thanks. – Simon Hayter Dec 10 '21 at 20:14