Jump to content

Date problem!


regoch

Recommended Posts

<?php
    $dayNames = array("Nedelja", "Ponedeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota");  
    $dan = $dayNames[date('N')];  
echo $dan.", ".date('d.m.Y.') ;
?>

This is my code for showing days in my language, but it works on every day except sunday!  Does anybody have any clue?

Thanks!

Link to comment
Share on other sites

Arrays are indexed from 0 by default, making your array indexed from 0 - 6, but date('N') returns a number in the range 1-7.

 

<?php
    $dayNames = array("Nedelja", "Ponedeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota");  
    $dan = $dayNames[date('N')-1];  # <-- Subtract 1 from the result of date('N')
echo $dan.", ".date('d.m.Y.') ;
?>

 

Edit: Sorry, you may need to re-order your array to make that work.  The "Subota" will need to go to the start, everything else would be in the same position.

Link to comment
Share on other sites

From the PHP manual entry for date:

N    ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0)    1 (for Monday) through 7 (for Sunday)

 

But your array indices start at 0. Start them at 1, and re-order them like this:

$dayNames = array( 1 => "Ponedeljak", "Utorak", "Srijeda", "Četvrtak", "Petak", "Subota", "Nedelja" );

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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