Otaku Kyon
Otaku Kyon

Reputation: 445

PHP: Finding the correct algorithm for date(); with time difference in a year

I am currently working on an algorithm to autoplay a unique audio-message for each day of a year, which is spoken by one of seven different persons depending on the day of the week.

For example: On May 7th the audio file 05-07.mp3 will be autoplayed and a Barack Obama image with the name 04.png is shown, because it is Thursday. The day after tomorrow is May 8th so 05-08.mp3 gets played, with Angela Merkel as 05.png.

Now here's the problem:

The beginning of this year was a Thursday, next year January 1st is a Friday. So if I use my script as I explained, Angela Merkel with image 05.png would use the voice of Barack Obama with 01-01.mp3 ..

How can I fix my script, so that I can create my audio files with dates in it's filename? What do I have to change? Another problem is that next year is also a February 29th!

Currently my script uses this php code:

<audio><source src="/mp3/<?php echo date("j"); echo "-" . date("m"); ?>.mp3" /></audio>

<img src="/png/<?php echo date("N"); ?>.png">

Upvotes: 1

Views: 68

Answers (1)

Gabriel Moretti
Gabriel Moretti

Reputation: 676

You should use a database to store the mp3 and the respective names. If you can't use a database, alternatively you can rename the sound's filename to something like obama_01-01.mp3 and then you can check.

@edit

$soundname = 'obama_01-01.mp3';
$imagename = explode('_',$soundname)[0];

$imagename .= '.png'

Then you rename your images' names to obama.png and so on

@edit2

to print the sound you can use

glob("*_01-01.mp3");

PHP: glob - Manual

PHP: explode - Manual

Upvotes: 1

Related Questions