Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Robert Walker
Robert Walker
17,146 Points

Unix timestamps, converting and math.

Having a little problem with how to work this out, little background and code to help:

$meetTimes = array(


                      "day" => "Monday",
                      "start_time" => "1416005281",
                      "finish_time" => "1416005281",
                      "room" => "5"


);



foreach ($meetTimes as $meeting){

echo '<h5>XX minutes from now</h5>';
echo '<h5>' . $meeting['day'] . '' . $meeting['start_time'] . '</h5>';
echo '<h5>' . $meeting['room'] . '</h5>';


}

Tasks I need to do:

1) Get a current timestamp to calculate how many minutes from now the nearest meeting is going to take place from the meetTime - starttime value. Not sure how to do this at all, do I need to make the current timestamp a unix number too so I can calculate the difference between or is there a better way to do it?

2) Convert the Unix start_time to a 24 hour number 16:23 - 21-20 etc but I im also looking at maybe dragging the day out of it instead of using the meetTime day value if that's possible.

3) Once the meeting is finished I need to move that meeting from the top of the current meeting list and place it at the end of the list repeating this through the day for each meeting as it completes.

Im not too sure how to do all this as the unix timestamp is throwing me completely.

Just as a last question, do Unix timestamps feature heavily in a lot of code? As ive already seen a fair few different ways that people deal with dates and times and im a little confused on how I should approach time and date in the future as I learn how to code more.

1 Answer

1) If your next meeting is at November 24th at 12:00, the timestamp would be 1416830400. To calculate how long it is from right now until then, just do:

$timeLeft = $nextMeeting - time();

that would give you a result of 353760 (approximately). That number isn't any random number - it's actually seconds. So there are about 350,000 seconds until November 24th. With a bit of math you can easily calculate how many minutes or hours that is - depending on what you want.

2) You can easily format a date output with php from just any Unix timestamp. Have a look at this:

http://php.net/manual/en/function.date.php

So if you want to output the date in the following format - November 24 at 12:00, you can do:

echo date("F d \a\t H:i", $timestamp);

3) I'm not sure I understand you here, but unless I'm mistaken, you are trying to resort an array after each meeting? If so, I'd advice against this method as it isn't necessary. I would just go through each of the elements in the array and for each one check if the current date-time is greater than the meeting time. If yes, the meeting has been held and we can move on to the next. If no, then this is the next meeting.

4) Unix timestamps are used alot! It is very simple to understand as well. It's an integer that shows how many seconds have passed since January 1st 1970. :)

Robert Walker
Robert Walker
17,146 Points

Thanks for the quick reply and it makes a lot more sense to me now.

Dates and times have thrown me a lot recently and is why ive sort of made this project for myself to learn but its still confusing me even when I lay out something that should be so simple.

3) Is to show the meetings that have taken place.

For example:

You have 12 meetings today on a webpage, first one starting in 10 minutes that's going to last for 40 minutes, once that finishes I want to move this from the top placing it at the bottom and will have a green border to show this meeting has taken place.

The whole project is to show real-time data on all the meetings taking place today, it works by showing in minutes how many minutes till the meeting, if over an hour its uses hours rather than minutes and if its complete within in a hour it says this meeting took play 44 minutes ago and if over an hour its goes in hours, this meeting took place X amount of hours ago.

The reason I need to add the meetings to the bottom is so that if a meeting does not actually take place someone can update this list to show that the meeting was cancelled but if all went well and it wasnt cancelled it just moves to the bottom of the list and styled a little different with a green border.

Okay, so this isn't something you should change in your array. It's just a matter of how you display them. Just do a loop through your array, and for each one check if they are in the future or in the past. If they are, display them at the top. If not, display them at the bottom.

Robert Walker
Robert Walker
17,146 Points

Actually ive just noticed something else too, I live the UK so when I look at the times they make no sense to me.

Is there a way to covert these into United Kingdom time somehow too?

I need to go read up more on all this its so confusing to me at the moment I cant think straight when I know each task im trying to do should be really simple.

Thanks for all the advice and help though Christian.

You are probably hosting off a server that's located outside your timezone. Servers are usually configured to display their own timezone. To change this, do:

date_default_timezone_set("Europe/London");

Should you ever need any other timezone, you can find the complete list of supported timezones here:

http://php.net/manual/en/timezones.php