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

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.