Any PHP gurus here ?

jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
I am trying for a while now, but I have given up :p

Is there a way, in PHP, to use the current date and time and print / show the remaining hours to the same day every week.

Basically display a counting on a site in php (doesn't need to refresh automatically) which shows for example the remaining hours until Friday 5pm .. every week, so it automatically detects the day of the week and calculates the time automatically until the coming Friday.

All I can find so far is a way to calculate the remaining hours based on a date given in the script
$datestr="2014-03-28 17:00:00";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));
echo "$days days $hours hours remain<br />";
My own knowledge base made public: http://open902.com :p

Comments

  • vasyvasyvasyvasy Member Posts: 68 ■■■□□□□□□□
    So you basically want to get the current date and throw in the same php script...
    Could this work for you?


    $datestr="2014-03-28 17:00:00";//Your date
    $datestr=date("Y-m-d H:i:s");//Your current date
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    I don't normally work in PHP but this might help you get started

    $fridayDate= date("Y-m-d",strtotime("this friday"));
    $fridayTime = "17:00:00";
    $fridayEnd = $fridayDate . ' ' . @fridayTime;



    PHP: strtotime - Manual
    When you go the extra mile, there's no traffic.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    It's all a bit Chinese to me to be honest.

    @vasyvasy: Not just the current date, but automatically calculate the time between now and upcoming weekday

    @CarlSaiyed: Not sure it helps lol - barely play with PHP myself so I am not even sure what I am looking at ..

    Thanks though ..
    My own knowledge base made public: http://open902.com :p
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    Paste in my code then do an echo on $fridayEnd
    When you go the extra mile, there's no traffic.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    CarlSaiyed wrote: »
    Paste in my code then do an echo on $fridayEnd

    Ah I see .. Thanks .. might be able to bastardize something ................... eventually :s
    My own knowledge base made public: http://open902.com :p
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    If the echo of $fridayDate gave you the date of this coming friday and 5:00PM then you just need to do the date diff and you have what you need.
    When you go the extra mile, there's no traffic.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    CarlSaiyed wrote: »
    If the echo of $fridayDate gave you the date of this coming friday and 5:00PM then you just need to do the date diff and you have what you need.

    Yepp, thanks. Getting there :)
    My own knowledge base made public: http://open902.com :p
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Great stuff, I think I got it
    <?php
    $fridayDate= date("Y-m-d",strtotime("this friday"));
    $fridayTime = "17:00:00";
    $fridayEnd = $fridayDate . ' ' . $fridayTime;
    $date=strtotime($fridayEnd);
    $diff=$date-time();
    $days=floor($diff/(60*60*24));
    $hours=floor(($diff-$days*60*60*24)/(60*60));
    $minutes=floor(($diff-$days*60*60*24)/60);
    echo "$days days $hours hours $minutes minutes remain<br />";
    

    Just hitting F5 for a while see what happens :p

    Edit: ok, once the full hour is over, it displays the whole amount of minutes, so just need to do some more calculations
    My own knowledge base made public: http://open902.com :p
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    Good work!
    When you go the extra mile, there's no traffic.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Can't figure out the minutes at the moment. At the moment it seems to work only by days and hours. Must be missing something stupid :D
    My own knowledge base made public: http://open902.com :p
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    What code are you using?
    When you go the extra mile, there's no traffic.
  • jibbajabbajibbajabba Member Posts: 4,317 ■■■■■■■■□□
    Trying to fix the one in post #9, although I think I know how, might check it tomorrow. Don't have access to the server at the moment. Basically $minutes displays the total of hours in minutes left. So if 2 days 23 hours are left, it would display 2 days, 23 hrs and 1380 minutes rather than 2 days, 23hrs and say, 59 minutes. I think I just calculate $days, $hours back to minutes and substract the lot from the total of seconds remaining (of you know whatI mean). I am probably just thinking too complicated.
    My own knowledge base made public: http://open902.com :p
  • NotHackingYouNotHackingYou Member Posts: 1,460 ■■■■■■■■□□
    jibbajabba wrote: »
    Trying to fix the one in post #9, although I think I know how, might check it tomorrow. Don't have access to the server at the moment. Basically $minutes displays the total of hours in minutes left. So if 2 days 23 hours are left, it would display 2 days, 23 hrs and 1380 minutes rather than 2 days, 23hrs and say, 59 minutes. I think I just calculate $days, $hours back to minutes and substract the lot from the total of seconds remaining (of you know whatI mean). I am probably just thinking too complicated.

    This is because you have probably already used 1380 / 60 to figure out how many hours you have.

    If minutes left holds a value of 1380 the I would use $minutes = ($minutes % 60).

    If $minutes was 90 then the result of 90 % 60 would be 30.
    When you go the extra mile, there's no traffic.
Sign In or Register to comment.