Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 02-12-2003, 08:28 AM   #1 (permalink)
AOD
Registered User
 
Join Date: Feb 2003
Posts: 4
AOD is on a distinguished road
Question Assign value to a variable depending on time

How do I assign a value to a variable depending on the time of the day?

For example: If time is between 2am to 4pm, variable = 50.

How can I accomplish this?

Thanks for any help.
AOD is offline   Reply With Quote
Old 02-12-2003, 10:11 AM   #2 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
use the date function to figure out what time it is.

PHP Code:
<?
  $x 
date("G");
  if(
$x >= && $x 16$tvar 50;
  else 
$tvar 0;
?>
joe_bruin is offline   Reply With Quote
Old 02-12-2003, 10:16 AM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
PHP Code:
<?
// get the current time
$current_time=time();
echo 
$current_time;

// format time to current hour
// using "G" format for current hour 0-23
$current_hour=date("G",$current_time);
echo 
"<br>" $current_hour;

// set variable based on hour
if($current_hour == 14 || $current_hour == 15) {
  
$var 50;
  echo 
"<br>" $var;
}
?>
here's a good reference for the date() function
http://www.php.net/manual/en/function.date.php
sde is offline   Reply With Quote
Old 02-12-2003, 10:47 AM   #4 (permalink)
AOD
Registered User
 
Join Date: Feb 2003
Posts: 4
AOD is on a distinguished road
Thanks joe_bruin and sde!

That would get me started.

Err, how do I use the date function to return the value in minutes or seconds? If I want to breakdown the variable assignment in 30-minute intervals for example...
AOD is offline   Reply With Quote
Old 02-12-2003, 11:13 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
http://www.php.net/manual/en/function.date.php

look at that link, and ask if you have questions. that has all your info.
sde is offline   Reply With Quote
Old 02-12-2003, 01:42 PM   #6 (permalink)
AOD
Registered User
 
Join Date: Feb 2003
Posts: 4
AOD is on a distinguished road
I want the date function to return values in seconds or minutes for a day instead of just 0 to 23 (using the "G" format).


Like so:

0 to 1440 (one whole day by the minutes)

0 to 86400 (one whole day by the seconds)


For example between 7:16 am to 7:46 am, variable = 12. The "G" format is inadequate for this.

Any ideas?
AOD is offline   Reply With Quote
Old 02-12-2003, 01:54 PM   #7 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
basic algebra?
joe_bruin is offline   Reply With Quote
Old 02-12-2003, 02:48 PM   #8 (permalink)
joe_bruin
LOAD "*",8,1
 
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
joe_bruin is on a distinguished road
the one line answer

PHP Code:
<?
  
eval("\$i = ".date("s + (60 * (i + (60 * G)))").";");
  echo 
"got $i\n";
?>
joe_bruin is offline   Reply With Quote
Old 02-12-2003, 02:55 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
here's 2 methods:

the first method we'll get the current hour, minutes, and seconds, then do some math to add them together:
PHP Code:
<?
$current_time
=time(); 

// set seconds 
$seconds date("s",$current_time); 

// set minutes 
$minutes date("i",$current_time); 

// set hours 
$hours date("H",$current_time); 

// calculate how many seconds in the minutes, and add to seconds 
$seconds $seconds + ($minutes 60); 

// calculate how many seconds in the hours, and add to seconds 
$seconds $seconds + ($hours 60) * 60

echo 
$seconds;
?>
second method we will get the time of today at midnight, .. then subtract that from the current timestamp:
PHP Code:
<?
// set the midnight date stamp for today
$midnightdate("Y-m-d") . " 0:0:0";

// set the current timestamp
$now=time();

// subtract now from midnight to get total seconds for today
$total_seconds_for_today=$now strtotime($midnight);

echo 
$total_seconds_for_today;
?>
both these examples will output the same thing.
sde is offline   Reply With Quote
Old 02-12-2003, 02:57 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
=) or joes way .. hehe , i wonder how many ways we can do the same thing.
sde is offline   Reply With Quote
Old 02-12-2003, 03:06 PM   #11 (permalink)
AOD
Registered User
 
Join Date: Feb 2003
Posts: 4
AOD is on a distinguished road
Figured that one out. Thanks you are all so helpful.
AOD is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
network time synchronisation in java bossebo Java 1 06-21-2004 05:17 PM
Adjusting date() function for time zones Epsilon PHP 1 02-27-2004 02:55 AM
Changing variable type for edit box : mfc - .net sde Standard C, C++ 0 01-31-2003 09:53 AM


All times are GMT -8. The time now is 05:08 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting