gettimeofday() how to use it?

A

Anonymous

Guest
Forgive my ignorance. I have only a very basic understanding of PHP.

I need to tell when the server time is after 5pm. I've read the explanation of gettimeofday() but I can't understand it. I'm not bothered about seconds or even minutes. I just need to know if it's between 5pm and 5am when my script is run. Can somebody please give me a simple example?

Martin
 
OK, this is obviously beyond me because I don't know how to call a function. I see this script as being probably one that could form the basis:

Code:
This function returns the correct DST time for EU countries. Change $TZOffset for your timezone. Use myDST() instead of time().

Other examples of usage:
getdate(myDST());
date('H:i:s', myDST());
getdate(myDST());

  function myDST()
  {
   $TZOffset = 1;

   // MEZ = UTC + TZOffset:
   $myMEZ = mktime(gmdate("H") + $TZOffset);
   $myMESZ = mktime(gmdate("H") + $TZOffset + 1);
   $myMEZ_year = date("Y", $myMEZ);

   $tmp = strtotime("last Sunday", mktime(0, 0, 0, 4, 1, $myMEZ_year));
   $myLastSundayMarch = mktime(2, 0, 0, date("m", $tmp), date("d", $tmp), date("Y", $tmp));

   $tmp = strtotime("last Sunday", mktime(0, 0, 0, 11, 1, $myMEZ_year));
   $myLastSundayOctober = mktime(3, 0, 0, date("m", $tmp), date("d", $tmp), date("Y", $tmp));

   if ( ($myMEZ >= $myLastSundayMarch) && ($myMESZ < $myLastSundayOctober) )
   {
     return $myMESZ; // summertime
   }
   else
   {
     return $myMEZ; // normal time ("wintertime")
   }
  }

But I don't know how to use it so I end up with something like:

if (5pm-5am){
$night_time = true;
}

Can I put this to somebody and pay them to provide a fully working script?
It needs to work on a UK server and it must handle GMT and BST automatically.

Martin
 
Basically you're going to use date() to get the current hour. Then you're going to compare it to see if it's greater than 17 (5pm) or less than 5 (5am).

Code:
<?php
  function time_between_5_and_5() {
    $hour = date('G'); // returns the hour (0-23)
    return ($hour >= 17) || ($hour < 5);
  }
?>

The function will return true of the time is after 5pm and before 5am, and false otherwise.
 
Sorry, this is obviously beyond my capabilities. Too many elements that I don't understand. I'm happy to pay somebody to give me the full working script. :)

Martin
 
Martin Pickering said:
Sorry, this is obviously beyond my capabilities. Too many elements that I don't understand. I'm happy to pay somebody to give me the full working script. :)

Martin
what is so difficult in swirlee's quote
 
Clearly we do not have an actual programmer on our hands. Sometimes we forget how hard these DIY projects can be.
 
I paid a professional to write the script for me. It seems to work OK and I know enough PHP to incorporate it into my existing scripts. I don't mind sharing it in case any other newbies need it.

Code:
<?php

// Created this function as I wasn't sure where you'd be running it, 
// my servers stay permanently on GMT and don't switch with DST.
// This BSTCheck function means it should work anywhere

function BSTCheck() {
	$ThisYear 	= (date("Y"));
	$MarStartDate 	= ($ThisYear."-03-25");	
	$OctStartDate 	= ($ThisYear."-10-25");
	$MarEndDate 	= ($ThisYear."-03-31");
	$OctEndDate 	= ($ThisYear."-10-31");

	//work out the Unix timestamp for 1:00am GMT on the last Sunday of March, when BST starts
	while ($MarStartDate <= $MarEndDate) { 
  		$day = date("l", strtotime($MarStartDate));
  		if ($day == "Sunday")
  			$BSTStartDate = ($MarStartDate);
		$MarStartDate++;
   	}
   	$BSTStartDate = (date("U", strtotime($BSTStartDate))+(60*60));
   	
   	//work out the Unix timestamp for 1:00am GMT on the last Sunday of October, when BST ends
   	while ($OctStartDate <= $OctEndDate) {
        	$day = date("l", strtotime($OctStartDate));
        	if ($day == "Sunday")
        		$BSTEndDate = ($OctStartDate);
   		$OctStartDate++;
   	}
   	$BSTEndDate = (date("U", strtotime($BSTEndDate))+(60*60));

   	//Check to see if we are now in BST
	$now = mktime();
	return (($now >= $BSTStartDate) && ($now <= $BSTEndDate)) ? TRUE : FALSE; }


$DST = (BSTCheck()) ? 3600 : 0;
$time = gmdate("H:i", time() + $DST);
$hour = gmdate("H",time() +$DST);
$day =  gmdate("w",time() +$DST);
$night = (($hour>=17) OR ($hour<5)) ? TRUE : FALSE;
$weekend = (($night AND ($day==5)) OR ($day==6) OR ($day==0) OR ($night AND ($day==1))) ? TRUE : FALSE;

echo ($DST . "<p></p>");
echo ("Time: " . $time . "<p></p>");
if ($night) {
echo ("Night time" . "<p></p>");
}else{
echo ("Day time" . "<p></p>");
}
if ($weekend) {
echo ("Weekend");
}else{
echo ("Weekday");
}

?>

As it stands, it will simply display the results on a web page but it's easy to adapt and it automatically accounts for GMT/British Summer Time changes (I use it in the UK). I suppose it might need modification if you use it elsewhere.

Click here to try it: http://www.The-Cool-Book-Shop.co.uk/example/gettime2.php

Martin
http://www.The-Cool-Book-Shop.co.uk
 
is it just me or there was a much easier way, or atleast shorter way of writing this?!
Cuz i think i had such a script somewhere and it was shorter..
 
I got a professional to write it, not a perfectionist. ;)

However, I have great faith in him because he can type in grammatically correct English and knows when to use a shift key. That suggests to me that he'll be better at typing in PHP without making mistakes.
 
Okej... Actually what defines a professional is being both a prefectionist by writing short, easy to extend codes, and being logical: writing a code without any overheads and useless operations. I never said that the code is wrong, not working or anything simmilar. I just said that I believe there is another way of doing it which will make the code smaller. But that doest really matter... just was my opinion.

And trust me: Even if a person can write in English without any misstakes (typos shouldn't be countes) -- it doesnt mean that he will be better in PHP coding then someone who makes some misstakes. As PHP is not about English: its about logical way of thinking, finding opimized ways of solving problems when writing all of the algorithms. It's about imagination :) True PHP programmers find the way of expressing themselved in their codes :) Think about it.. And if a person says that he or she is a professionall, or even is working in some company that works with PHP coding: there are people who always are better in some areas. But this is a bit :eek:fftopic:

I hope you agree with me :)

By the way: if you've waited couple of days: you would've recieved that code (well the code that does the same) for free :)
As its a very simple code and i simply didnt have time to write a reply (exams and then lots of work) :)

Well, glad that you've got your code now :)
 
Thanks, Alexei. As nobody had offered, I decided to pay. It was a trivial amount.

In fact the $weekend function isn't working so I've asked him to look at it.

I've replaced the example, above, with the corrected version.
 
Back
Top