Home > OS >  PHP getdate echo if yday equals
PHP getdate echo if yday equals

Time:12-11

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.

CodePudding user response:

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];
  • Related