SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem with php generated select

    Hi,

    I have a <select> , a friend on sitepoint gave me.

    I just find out the date it generates isn't the real date on calendar, probably problem lieing under my server config . can you help me check it ?

    Code PHP:
    <?php
    $aTranslated = array(
        'french' => array(
            'Lundi',
            'Mardi',
            'Mercredi',
            'Jeudi',
            'Vendredi',
            'Samedi',
            'Dimanche',
        ),
        'english' => array(
            'Monday',
            'Tuesday',
            'Wednesday',
            'Thursday',
            'Friday',
            'Saturday',
            'Sunday',
        ),
    );
     
    echo '<select name="end">';
    echo '<option value="0">date/month</option>';
    for ($iCounter = 1 ; $iCounter <= 10 ; $iCounter++ )
    {
        $iTimeStamp = strtotime(sprintf('+%s Days',$iCounter));
        echo sprintf('<option value="%s" >%s %s</option>',
            $iCounter,
            $aTranslated['english'][date('w',$iTimeStamp)],
            date('d/m',$iTimeStamp)
        );
    }
    echo '</select>';
    ?>

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    You could try setting the the correct timezone prior to generating the select.

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The function date_default_timezone_set() didn't help. I tried it with my city identifier but no good.

    I think even though the timezone is not the same, the option should just slightly 24 hours faster or slower than my area time.But

    What I have here is so unreal , thurday 31/12, friday 01/01, saturday 02/01 and so on( those dates are not existing in the calendar)

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its ok now

    I fix it like this

    Code PHP:
    ...for ($iCounter = 0 ; $iCounter <= 10 ; $iCounter++ )
    						{
    							$iTimeStamp = strtotime(sprintf('+%s Days',$iCounter));
    							echo sprintf('<option value="%s" %s %s>%s %s</option>',
    								$iCounter +1,..

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's probably if you use date('w'), you get 0 for Sunday, and in you array you have 0 for Monday.

  6. #6
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its ok now

    I fix it like this

    Code PHP:
    ...echo '<option value="0">date/month</option>';
    		for ($iCounter = 0 ; $iCounter <= 10 ; $iCounter++ )
    			{
    			$iTimeStamp = strtotime(sprintf('+%s Days',$iCounter));
    			echo sprintf('<option value="%s" %s %s>%s %s</option>',
    			$iCounter +1,..

    I put $icounter = 0 instead of = 1

    And in sprintf parameter i put $counter +1

    So that I don't have two value = 0 in my select and also solve the wrong day problem

  7. #7
    SitePoint Zealot
    Join Date
    Mar 2008
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, it works, but I think that better solution is to either:
    1. in your $aTranslated array, move sunday before monday
    or
    2. insted of
    PHP Code:
    $aTranslated['english'][date('w',$iTimeStamp)], 
    use
    PHP Code:
    $aTranslated['english'][date('w',$iTimeStamp) + 1], 

  8. #8
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Fixed it, the translated entries were indexed incorrectly.

    PHP Code:
    <?php
    $aTranslated 
    = array(
        
    'french' => array(
            
    'Dimanche',
            
    'Lundi',
            
    'Mardi',
            
    'Mercredi',
            
    'Jeudi',
            
    'Vendredi',
            
    'Samedi'
        
    ),
        
    'english' => array(
            
    'Sunday',
            
    'Monday',
            
    'Tuesday',
            
    'Wednesday',
            
    'Thursday',
            
    'Friday',
            
    'Saturday'
        
    ),
    );
     
    echo 
    '<select name="end">';
    echo 
    '<option value="0">Day Date/Month</option>';
    for (
    $iCounter $iCounter <= 10 $iCounter++ )
    {
        
    $iTimeStamp strtotime(sprintf('+%s Days',$iCounter));
        echo 
    sprintf('<option value="%s" >%s %s</option>',
            
    $iCounter,
            
    $aTranslated['english'][date('w',$iTimeStamp)],
            
    date('d/m',$iTimeStamp)
        );
    }
    echo 
    '</select>';
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    **Double Post**
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •