Forum Moderators: coopster

Message Too Old, No Replies

How can PHP detect what time or day it is? (I don't just mean UNIX)

How can PHP be used to detect the exact time and day?

         

php_dave

3:16 am on Sep 2, 2011 (gmt 0)

10+ Year Member



Hello.

How do I use PHP to detect the actual day and time? Obviously the UNIX timestamp can be turned into the current date but how can I use that to turn the current day or time into a variable?

What I mean is this. I'm building a website for a shop and I want the front page to say whether or not the shop is open at that moment.

How can I use PHP to detect that it's after 9:00am and before 5:00pm on a Monday, for example, so that the website says OPEN!

And how can I use PHP to detect that it's Wednesday so it can display something only on Wednesdays?

Thanks in advance to anyone who can shed some light on this matter :)

incrediBILL

3:36 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This array of data shoulf help [php.net...]

php_dave

4:17 am on Sep 2, 2011 (gmt 0)

10+ Year Member



Thanks, Bill. But I'm still confused.

The page says:


<?php
$today = getdate();
print_r($today);
?>


The above example will output something similar to:


Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)


...and it does! But how do I echo just the weekday, for example?

My apologies if this is really basic stuff, I've just not dealt with this before :s

lucy24

4:37 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you worked with arrays in any other context? Then you know that once you've got the array, you can request any one of its elements. I won't or rather can't write out the actual code, because I don't speak php, but all computer languages have a way of doing it.

Set up a dummy page and experiment. See what you get if you ask for only item #4, or #1, or #9. Note that the "hours" item in the example uses 24-hour time, so you only have to look for >= 9 and <17. And some pieces of information, like the month, come in two forms-- as numbers and as strings-- so you can use whichever one is more useful to you. One version for computation, the other for user display.

Does "getdate" give you the local time by default? Including summer time (DST) if any? If you're talking about a single shop in a single location, that's all you need. It would be messier if you were scattered all over Canada and had to work with the user's time zone.

incrediBILL

5:01 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RTM ;) [phptutorial.info...]

php_dave

5:16 am on Sep 2, 2011 (gmt 0)

10+ Year Member



Hah, I've got it!

This is the format I was looking for.

<? if (date("l") == 'Friday') { ?>It's Friday<? } ?>


Previously I'd been trying things like
<? if ($date->l == 'Friday') { ?>


I don't think I'd have worked out the correct format just using trial and error.

Ahh fantastic. Thanks, people! :)

g1smd

7:59 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you only sell within your own country? If I am located the other side of the world, you'll always tell me that your store is closed.

Be sure to suppress this open/closed text when you serve pages to Google etc. You absolutely do NOT want "this store is now closed" to appear in the snippets of your search results.

php_dave

8:03 am on Sep 2, 2011 (gmt 0)

10+ Year Member



Thanks for the suggestion but it's for a fastfood takeaway so international time zones won't be necessary this time :P

Good point about the closed text! I'll block it from Google Bot.

penders

8:20 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Related to time zones again... since you will be using this script to determine if your local shop is open. The date() function returns a date/time with respect to local time - that's local to your web server. If your web server and your shop are in the same time zone (or at least set to the same time zone) then all is good. If not then you will need to adjust to the time zone of wherever your shop is.

php_dave

8:35 am on Sep 2, 2011 (gmt 0)

10+ Year Member



One step ahead of you, Penders! I've already added the necessary 5 hours on (I'm in the UK, the server's in the US).

g1smd

9:11 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Watch out for when the US comes off (and goes on) DST and when the UK comes off (and goes on) BST.

They are not on the same date and you could find a one hour error for several weeks each year.

The easiest way to handle this is to first get the server time and convert it to UTC in your script, then add +0 in the UK winter and +1 in the UK summer.

See also: [timeanddate.com...]

penders

9:31 am on Sep 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've already added the necessary 5 hours on (I'm in the UK...


Forgive me, you may be doing this OK already, but you shouldn't be manually adding/subtracting hours as this might not take into account DST. Being in the UK, if you call date_default_timezone_set('Europe/London') [uk2.php.net] at the start of your script then PHP will handle all the time zone differences for you.

(I didn't see @g1smd's post above)

php_dave

3:19 am on Sep 3, 2011 (gmt 0)

10+ Year Member



G1smd and Penders, thanks for the heads up. I was aware that daylight saving hours would cause problems somewhere along the line but I wasn't sure how to solve it.

Where do I put the date_default_timezone_set('Europe/London')? When I put it onto my page and refresh it the page doesn't load! (and yes, I am putting it inside php tags :P)

Websites say it just needs adding to the top of the php page but that doesn't work for me. Waaah :(

penders

10:40 am on Sep 3, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where do I put the date_default_timezone_set('Europe/London')? When I put it onto my page and refresh it the page doesn't load!


Are you running PHP 5? Do you get any errors? Make sure you have full error_reporting set...
error_reporting(E_ALL); 
ini_set('display_errors','On');


As mentioned (if on PHP 5) it just needs to go at the start of your script, before you call any date/time functions. Although you should get a warning if this isn't already set?!

Readie

5:55 pm on Sep 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$today = getdate();
print_r($today);
?>


The above example will output something similar to:


Array
(
[seconds] => 40
[minutes] => 58
[hours] => 21
[mday] => 17
[wday] => 2
[mon] => 6
[year] => 2003
[yday] => 167
[weekday] => Tuesday
[month] => June
[0] => 1055901520
)


...and it does! But how do I echo just the weekday, for example?

My apologies if this is really basic stuff, I've just not dealt with this before :s


I know the thread has moved past this, but, just so you know how to interact with arrays:

echo $today['seconds'];