SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: PHP Question

  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Question

    I am working on a radio panel, where DJ's can log in and book a slot to go on air. The only issue is - that if someone books a slot at 3AM GMT, and then changes their timezone to GMT+10, it goes backwards where it should go forward..

    PHP Code:
    $query8  = $db->query( "SELECT * FROM timezones WHERE title = '{$user->data['timezone']}'" );
    $array8  = $db->assoc( $query8 );
     
            define("My_TZ", "{$user->data['timezone']}");
            function TZ_Math($Num)
            {
                    if(My_TZ == "GMT")
                    {
                            $o = $Num;
                            for($i = 0; $i > 0; $i++)
                            {
                                    if($i < 23)
                                    {
                                            $o++;
                                    }
                                    else
                                    {
                                            $o = 0;
                                    }
                            }
                           
                            return $o;
                    }
                    else
                    {
                            return $Num;
                    }
            }
           
                    $query5 = $db->query( "SELECT * FROM days ORDER BY id" );
                    $num5   = $db->num( $query5 );
     
     
                    while( $array5 = $db->assoc( $query5 ) ) {
                    $i = $array5['day'];
                    $day = strtotime( "{$i} october 2012" );
                    $day = date( "l", $day );
                   
    ?>
     
    <div class="box">
     
            <div class="square title" style="margin-bottom: 0px; cursor: pointer;" onclick="Radi.timetableToggle('<?php echo $i?>'); return false;">
                   
                    <img id="toggle_<?php echo $i?>" class="toggle" src="_img/<?php echo $i == "minus" "plus"?>.png" alt="Toggle" align="right" />
     
                    <strong><?php echo $day?></strong>
                   
            </div>
     
            <div id="day_<?php echo $i?>" class="day"<?php if($i != ) { ?> style="display: none;"<?php ?>>
                   
                    <table width="100%" cellpadding="3" cellspacing="0">
                   
                            <tr>
                           
                                    <td width="20%" valign="top">
                                    <?php
                                           
                                            $k 
    1;
                                           
                                            for( 
    $j 0$j <= 23$j++ ) {
                                                   
                                                    if( 
    $j 10 ) {
                                                   
                                                            
    $time "0{$j}:00";
                                                   
                                                    }
                                                    else {
                                                   
                                                            
    $time "{$j}:00";
                                                   
                                                    }
     
                                                    if( 
    $array8['symbol'] == "-" ) {
     
                                                            
    $tza TZ_Math($j) - $array8['value'];
     
    if (
    $tza == "24:00" || $tza == "25:00" || $tza == "26:00" || $tza == "27:00" || $tza == "28:00" || $tza == "29:00" || $tza == "30:00" || $tza == "31:00" || $tza == "32:00" || $tza == "33:00" || $tza == "34:00" || $tza == "35:00" || $tza == "36:00"){
    if (
    $day == "Monday"){
    $i 2;
    }
    else if (
    $day == "Tuesday"){
    $i 3;
    }
    else if (
    $day == "Wednesday"){
    $i 4;
    }
    else if (
    $day == "Thursday"){
    $i 5;
    }
    else if (
    $day == "Friday"){
    $i 6;
    }
    else if (
    $day == "Saturday"){
    $i 7;
    }
    else if (
    $day == "Sunday"){
    $i 1;
    }
    }
    if (
    $tza == "24:00"){ $tza "0";}
    if (
    $tza == "25:00"){ $tza "1";}
    if (
    $tza == "26:00"){ $tza "2";}
    if (
    $tza == "27:00"){ $tza "3";}
    if (
    $tza == "28:00"){ $tza "4";}
    if (
    $tza == "29:00"){ $tza "5";}
    if (
    $tza == "30:00"){ $tza "6";}
    if (
    $tza == "31:00"){ $tza "7";}
    if (
    $tza == "32:00"){ $tza "8";}
    if (
    $tza == "33:00"){ $tza "9";}
    if (
    $tza == "34:00"){ $tza "10";}
    if (
    $tza == "35:00"){ $tza "11";}
    if (
    $tza == "36:00"){ $tza "12";}
     
                                                    }elseif( 
    $array8['symbol'] == "+" ){
                                                           
                                                            
    $tza TZ_Math($j) + $array8['value'];
     
    if (
    $tza == "24:00" || $tza == "25:00" || $tza == "26:00" || $tza == "27:00" || $tza == "28:00" || $tza == "29:00" || $tza == "30:00" || $tza == "31:00" || $tza == "32:00" || $tza == "33:00" || $tza == "34:00" || $tza == "35:00" || $tza == "36:00"){
    if (
    $day == "Monday"){
    $i 2;
    }
    else if (
    $day == "Tuesday"){
    $i 3;
    }
    else if (
    $day == "Wednesday"){
    $i 4;
    }
    else if (
    $day == "Thursday"){
    $i 5;
    }
    else if (
    $day == "Friday"){
    $i 6;
    }
    else if (
    $day == "Saturday"){
    $i 7;
    }
    else if (
    $day == "Sunday"){
    $i 1;
    }
    }
    if (
    $tza == "24:00"){ $tza "0";}
    if (
    $tza == "25:00"){ $tza "1";}
    if (
    $tza == "26:00"){ $tza "2";}
    if (
    $tza == "27:00"){ $tza "3";}
    if (
    $tza == "28:00"){ $tza "4";}
    if (
    $tza == "29:00"){ $tza "5";}
    if (
    $tza == "30:00"){ $tza "6";}
    if (
    $tza == "31:00"){ $tza "7";}
    if (
    $tza == "32:00"){ $tza "8";}
    if (
    $tza == "33:00"){ $tza "9";}
    if (
    $tza == "34:00"){ $tza "10";}
    if (
    $tza == "35:00"){ $tza "11";}
    if (
    $tza == "36:00"){ $tza "12";}
                                                           
                                                    }else{
                                                            
    $tza TZ_Math($j);
                                                    }
                                                   
                                                    
    $query $db->query"SELECT * FROM timetable WHERE day = '{$i}' AND time = '{$tza}'" );
                                                    
    $array $db->assoc$query );
                                                    
    $num   $db->num$query );
     
                                                    
    $query2 $db->query"SELECT * FROM users WHERE id = '{$array['dj']}'" );
                                                    
    $array2 $db->assoc$query2 );
     
                                                    
    $query3 $db->query"SELECT * FROM usergroups WHERE id = '{$array2['displaygroup']}'" );
                                                    
    $array3 $db->assoc$query3 );
     
                                                    echo 
    "<div style=\"padding: 3px;\">";
                                                   
                                                    echo 
    $time;
                                                    echo 
    " - ";
                                                   
                                                    if( 
    $num == ) {

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That code looks terribly complicated. PHP has a wonderful date and time functionality and I see no reason not to use it. Universal Time (GMT) is the same everywhere, what changes is the local time. If you give php a time stamp, it will calculate the right local time for you. You set your time zone with date_default_timezone_set()
    http://www.php.net/manual/en/functio...mezone-set.php

    Let me know if it works for you.
    Denny Schlesinger
    web services

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Essentially, that would replace the for loop, correct?

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is also so much that is dependent on that.. There is so much code that would have to be changed to take that out.. I was just wondering if there was a quick fix to actually make it work by going both + and - and not just -.

    Thanks for your help!

  5. #5
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,411
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    There is also so much that is dependent on that.. There is so much code that would have to be changed to take that out.. I was just wondering if there was a quick fix to actually make it work by going both + and - and not just -.
    You will regret not doing it properly now as it will come back to haunt you later.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed out the for loop with the date_default_timezone_set..

    How does that automatically change the timezone? Will it just do it automatically or is there more code involved?

    Sorry - learning..

  7. #7
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    516
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    What a strange coincidence. I was watching a 1951 movie about Tyrone Power going back from 20th century New York to 18 century London (a bit like a Connecticut Yankee in King Arthur's court) while being asked about a script about time. Weird indeed!

    Sorry, I did not look at the script in any detail, I just noticed that it is very complicated and it seems you also do some scripting in MySQL which I try to avoid. At this point I don't understand the procedure you are trying to set up. It seems that people sign in for a time slot. If this is a world wide script, I would set up the time slots in Universal Time or GMT (essentially the same thing). People signing in would have to pick their time zone, could be from a drop down menu. Then they can use local time because the time zone gives the difference to GMT including day light saving and all that jazz.

    Probably the best course is to throw out the old idea and start coding afresh. Knowing the time zones, php makes calculating times very easy if the whole thing if based on GMT.

    For example, 4 PM GMT for someone at GMT +10 would be

    4+12+10 = 26
    26 = tomorrow (24) + 2

    Tokyo is GMT +9, it is already Monday for them:
    Monday, 29 April 2013, 04:07:13 JST
    (I looked it up in http://www.timeanddate.com/worldclock/city.html?n=248 )
    Denny Schlesinger
    web services


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
  •