PHP - Calculating An Older Date/time Based On A Desired Interval
I've been researching online resources and seen a lot of code and functions for calculating an interval or difference between two date/time figures. What I'm trying to do is somewhat in reverse... I want to establish an interval to determine a past date/time to be used in a MySQL Query but just can't figure the critical part.
// STEP #1 = DEFINE THE DESIRED DATE/TIME DIFFERENCE INTERVAL DESIRED FOR QUERIES $timeinterval = '60'; // Interval is in MINUTES and can be changed as desired // STEP #2 = GET THE CURRENT LOCAL SERVER DATE/TIME // as YYYY-MM-DD HH:MN:SE $serverdatetime = date('Y-m-d H:i:s'); // My local Server Date & time // STEP #3 = CALCULATE THE ***PAST** (OLDER) DATE/TIME LIMIT BASED ON $timeinterval (Minutes) // as YYY-MM-DD HH:MN:SE $pastdatetime = [stuckinarut here] <- 60 MINUTES prior to current Server Date/Time // STEP #4 = MySQL QUERY (`submitted` column is auto-timestampped on record insertions) $sql="[columnsblahblah] FROM mydb WHERE `submitted` >= $pastdatetime";In a Perfect World, the MySQL Query would yield ONLY records with a `submitted` DATE/TIME equal to 60 Minutes (or less) PRIOR TO from the Current Server Time. Any assistance is appreciated! Thanks. -FreakingOUT Similar TutorialsI would really appreciate help and I hope my problem can be solved easily. I am attempting to display the date/time based on the user's location. I have installed the timezone.js plugin for help as well. Is there a way to convert the Jquery variable that displays the users timezone location and insert it inside the PHP date_default_timezone_set?
If there is an easier way to display the users timezone date and time please let me know otherwise this was the only solution I could find.
Thanks in advanced...
Here is my code
Header
<header> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script> <script src="Time/detect_timezone.js"></script> <script src="Time/jquery.detect_timezone.js"></script> <script> $(document).ready(function(){ $('#tzvalue').set_timezone({'default' : 'America/Los_Angeles'}); }); </script> </header> Hello, I tried to implement some PHP code to enable a web page to show "Dinner" specials versus "Lunch" specials based on the time. The business is located in the Eastern Time Zone, so I only need to routine to work based on Eastern Time (New York). What I am noticing is that the server is processing the lines of code so fast that the command to establish the correct time for the remaining code is not always being acknowledged. This line of code appears to be processing too fast for the remaining lines of code: date_default_timezone_set ( "America/New_York" ); Is there some additional code I can put in place to make sure the correct time is always ascertained? I need the $hourmin variable to always return a combination of hour + minute based on 24-hour time and Eastern Time zone. My code is as follows: <?php $name8 = file_get_contents("test/special8name.txt"); date_default_timezone_set ( "America/New_York" ); $gethour = date("H"); $getminutes = $gettimedate["minutes"]; $gettimedate = date(); $hourmin = $gethour . $getminutes; $currentday = date("l", time()); $currentdate = date("M j, Y"); if ($hourmin < 1500 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesred\">$name8 </span>"; } else if ( $hourmin > 1500 && $hourmin < 2300 && $currentday <> "Saturday" && $currentday <> "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } else if ( $currentday == "Saturday" or $currentday == "Sunday") { echo "<span class=\"namesblue\">$name8 </span>"; } ?> I'm working on a project that has a lot of different timezone options, and all I have from the users is their UTC offset. After weeks of doing tweaks, I come to new issues every time I add something, one would think it would be a lot easier to just get the correct time and day :) The latest issue is that when I try to rewrite a date to a better fomat, it disregards the GMT offset. Like this: 4/14/19, 12:00 PM GMT+2 (=$date) becomes ... 14-04-2019 10:00 when using this code: $bp_date_to_format = date_create_from_format('Y-m-d\TH:i:sP', $date); echo date_format($bp_date_to_format, 'd-m-Y H:i'); I figured it would just rewrite the format, keeping the exact time. It isn't. We do have all these sorts of localization functions as well, but because there doesn't seem to be one single standard (each 3rd party API connection we have uses its own default datetime format so it seems). // Remove UTC Text $UTC_offset = str_replace('UTC', '', $timezone); // Get Offset in Minutes if (stripos($UTC_offset, ':') !== false) { // Calculate seconds from offset list($hours, $minutes) = explode(':', $offset); $seconds = $hours * 60 * 60 + $minutes * 60; } else { $seconds = $UTC_offset * 60 * 60; } // Get User timezone name from seconds $timezone = timezone_name_from_abbr('', $seconds, 1); if ($timezone === false) { $timezone = timezone_name_from_abbr('', $seconds, 0); } // Set new TZ return date_default_timezone_set($timezone);
Isn't there any "one way"solution that can be used? It's confusing to say the least.
Cheers. Hi all. In my database, i have a column recurring which is derived from a multiple option form field with values: Weekly, Bi-Monthly, Monthly, Quarterly, Half Yearly and Yearly. I want to have as next due the current date plus the recurring value. eg current date = 2014-11-24 recurring = monthly next due = current date + recurring (in the next due will be 2014-12-24) so i did: $stmt = $pdo->query("SELECT recurring, due_date FROM $table"); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $recur = $row['recurring']; $date_due = $row['due_date']; } $weekly = "Weekly"; $bi_monthly = "Bi-Monthly"; $monthly = "Monthly"; $quarterly = "Quarterly"; $half_yearly = "Half Yearly"; $yearly = "Yearly"; if(strcmp($recur, $weekly) == 0){ $recurs = "7 DAY"; }elseif(strcmp($recur, $bi_monthly) == 0){ $recurs = "14 DAY"; }elseif(strcmp($recur, $monthly) == 0){ $recurs = "1 MONTH"; }elseif(strcmp($recur, $quarterly) == 0){ $recurs = "3 MONTH"; }elseif(strcmp($recur, $half_yearly) == 0){ $recurs = "6 MONTH"; }elseif(strcmp($recur, $yearly) == 0){ $recurs = "1 YEAR"; } $stmt = $pdo->query("SELECT ADDDATE('$date_due', INTERVAL $recurs) as nex_due FROM $table"); $row = $stmt->fetch(PDO::FETCH_ASSOC); $nex_due = $row['nex_due']; $stmt = $pdo->prepare("SELECT * FROM $table ORDER BY trans_id DESC"); $stmt->execute(); $num_rows = $stmt->rowCount(); while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['trans_ref']; echo "</td><td>"; echo $row['acct_num']; echo "</td><td>"; echo ucwords($row['payee']); echo "</td><td>"; echo ucwords($row['company']); echo "</td><td>"; echo $row['acct_no']; echo "</td><td>"; echo number_format($row['amt'],2); echo "</td><td>"; echo $row['purpose']; echo "</td><td>"; echo $row['recurring']; echo "</td><td>"; echo $row['due_date']; echo "</td><td>"; echo $nex_due; echo "</td><td>"; echo "<strong>".$row['status']."</strong>"; echo "</td><td>"; echo "<strong>".$row['pay_status']."</strong>"; echo "</td><td>"; } The problem is that it's giving me as next due the value of the first row even when the recurring is different! HI,
I was trying do add a function to my script to stop each 10 seconds, using max_execution_time. Apparently is not working and most probably from the php.ini file, that I can t change because I want for the others script to run normally.
How can I write a function inside a php file to stop everything after 10 seconds. Or how can I make a script to stop after 100 processed items and start again after 30 seconds. I need to stop this file somehow. $processedItems++; Does any one know how to run PHP script automatically at specific time interval in my local machine. I am using Windows 7. What about using the Task scheduler ? Please write to me in details how to give the path of the php page in Task Scheduler. guys, please help i have table with datas like speed of vehicle, position e.t.c from morning 8 a.m. to 8 p.m. I need to get these details, but after every 15 minutes i.e. after selecting datas at 8 a.m. it shd select datas @ 8.15 a.m. then 8.30 a.m. hw can i write a mysql query for this ? or a PHP approach is appreciated Hey guys, How would I go about subtracting Today from a previous day to find the difference? For example, I want to subtract TODAY from a previous date in my database, to determine if the difference is greater than 1 day. Any ideas? I tried doing the subraction in TIMESTAMPS, but when I convert the date back to Y-m-d H:i:s, I got some weird year and time. I have a table with Territory Representative Sales. It has columns that go back 26 months. The columns are SALES_OLDEST_PD_1, SALES_PD_2, SALES_PD_3, ..... SALES_PD_25, SALES_LAST_PD_26, & SALES_CURR_PD_27. SALES_CURR_PD_27 is the current month. I want to build a table with "Last Year To Date Sales" (LYTD), "Current Year To Date Sales" (CYTD), (May 2011), and (May 2010). I know that (May 2011) will be SALES_CURR_PD_27 and (May 2010) will be SALES_PD_15. The trick is that the sales year starts in November. So, I need to figure out a way to match the table columns with "Nov. 09 - May. 10 TOTAL" for (LYTD) and "Nov. 10 - May. 2011 TOTAL" for (CYTD). I am familiar with using mktime() to subtract months. But, I am not sure where to start yet on this project. Jake Code: [Select] while (odbc_fetch_row($rs)) { $M1 += odbc_result($rs,"SALES_OLDEST_PD1"); $M2 += odbc_result($rs,"SALES_PD2"); $M3 += odbc_result($rs,"SALES_PD3"); $M4 += odbc_result($rs,"SALES_PD4"); $M5 += odbc_result($rs,"SALES_PD5"); $M6 += odbc_result($rs,"SALES_PD6"); $M7 += odbc_result($rs,"SALES_PD7"); $mate += odbc_result($rs,"SALES_PD8"); $M9 += odbc_result($rs,"SALES_PD9"); $M10 += odbc_result($rs,"SALES_PD10"); $M11 += odbc_result($rs,"SALES_PD11"); $M12 += odbc_result($rs,"SALES_PD12"); $M13 += odbc_result($rs,"SALES_PD13"); $M14 += odbc_result($rs,"SALES_PD14"); $M15 += odbc_result($rs,"SALES_PD15"); $M16 += odbc_result($rs,"SALES_PD16"); $M17 += odbc_result($rs,"SALES_PD17"); $M18 += odbc_result($rs,"SALES_PD18"); $M19 += odbc_result($rs,"SALES_PD19"); $M20 += odbc_result($rs,"SALES_PD20"); $M21 += odbc_result($rs,"SALES_PD21"); $M22 += odbc_result($rs,"SALES_PD22"); $M23 += odbc_result($rs,"SALES_PD23"); $M24 += odbc_result($rs,"SALES_PD24"); $M25 += odbc_result($rs,"SALES_PD25"); $M26 += odbc_result($rs,"SALES_LAST_PD26"); $M27 += odbc_result($rs,"SALES_CURR_PD27"); } Note: By the way SMF is changing $M"8" to $M"ate". lol Hi, im trying to calculate and display a time elapsed from known date in db. however im having trouble displaying it. other values display no problem, just cant calculate and display date on the fly. does anyone see a problem with my query? thank you. Code: [Select] <?php include('config.php'); $uname=$_SESSION['username']; if($_SESSION['username']){ $sql="SELECT country, nationality, passportDate FROM country WHERE uname='$uname'"; $result = mysql_query($sql) or die(mysql_error()); $passportDate _ts = strtotime($row['passportDate ']); $passportDate _str = date("M-d-Y", $passportDate _ts); $TotalTime = floor((time() - $passportDate _ts)/(60*60*24)) . ' days'; while($row=mysql_fetch_array($result)) { echo "<table border='0'> <tr> <th>Nationality</font></th> <th>Country</font></th> <th>Passport Seent Date</font></th> <th>Time Elapsed</font></th> </tr>"; echo "<tr>"; echo "<td>" . $row['nationality']."</td>"; echo "<td>" . $row['country'] . "</td>"; echo "<td>" . $row['passportDate '] . "</td>"; echo "<td>" . ('$TotalTime') . "</td>"; echo "</tr>"; } echo "</table>"; } ?> I have collected some data from .xml api's and I need to manipulate it in a way to display something specific.
not my strongest point when it comes to maths & date data.
so ultimately I need it to echo something like the following:
"3 Days 4 Hours Remaining" or "14 Hours Remaining" (if below 24 hours!!!)
I use the following to pull my variables and to test by echo results.
<?php // Calculate total Fuel blocks $itemID = $record['itemID']; $result = mysql_query("SELECT * FROM `ecmt_poslistdetails` WHERE itemID = '$itemID' AND (typeID='4051' OR typeID='4247' OR typeID='4246' OR typeID='4312')"); $row = mysql_fetch_array($result); $fuelQty = $row[quantity]; echo $fuelQty; ?> <br /> <?php // Calculate total Fuel blocks used per hour $typeID = $record['typeID']; $result = mysql_query("SELECT * FROM `ecmt_posfuel` WHERE typeID = $typeID"); $row = mysql_fetch_array($result); $fuelUse = $row['fuel']; echo $fuelUse; ?>this gives me: $fuelQty & $fuelUse so for instance if fuel quantity is: 18480 and $fuelUse is: 40 the fuel used is 40x per hour and time remaining would be 462x Hours remaining but I want it to display: 19 Days 6 Hours Remaining how can I achieve this format? Hi guys! This will seem complex but I really need your help If a have a variable with a date value (i.e. $a = '23/10/2011') How do you work out the week number of that date? Thanks This topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=355963.0 Basically I have recently been playing around with parsing a csv file. What I am looking to do at this point is simply take the date/timestamp (part of the csv file), which is in the following format:DD/MM HH:MM:SS.100th/s For the sake of argument, lets say I have this in an array string called $csv[0] and the file has several lines that span the course of a couple hours. I wouldn't mind having to use explode() to breakup/remove the date or 100th/s IF that would make things a lot simpler. So where would I start in trying to achieve this?. The result I am looking for will simply return "X Seconds". Storing this in a string variable would be a bonus, as I plan to use this to divide a separate piece of information. Any examples or ideas would be great. Thank you. ps: Here is an example time from the csv file itself: Code: [Select] 11/19 22:23:18.143 PHP date and time function is not showing correct time on my local system I have the following php code date_default_timezone_set("Africa/Lagos"); $date = date('d-m-y h:i:s'); echo "Server Time ".$date ."<br>"; echo "The time is " . date("h:i:sa")."<br>"; $current_datetime = date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa'))); echo "Current time1: ".$current_datetime . "<br>";
Output
Server Time 21-05-21 09:55:39
Expected Output
Server Time 21-05-21 10:55:39
Any help would be appreciated. Edited May 21 by Ponel Hi Guys.. How can I change a date on the fly ? Everything is UTC on my server. How can I change a date to something else on the fly? Ie: $timezone = "cet"; $datetime = "2011-09-04 19:53:00"; echo $datetime($timezone); So I can give it a datetime and have it echo the datetime as if it were in the other timezone? Thanks Graham OK So I've got a datepicker that sends a date in d/m/y format. My DB stores the data in Unix Timestamp Which I can convert the date to with strtotime however this does the exact date & time. All I want is the actual day. I've spent hours trying to convert this with just the day with mixed results... Thanks. I am having a problem with PHP displaying the correct date and time. It updates as it should, but is fast by 4min and is always displaying a date in 2004. I ran a basic php script to make sure the application im using itself is not wrong. go to lunenburgledger.com/time.php Anybody had any ideas on where to check? The system time on the Windows Server 2003 is correct. The only thing I can think of is that it was converted to a virtual machine on vmware esxi, but the system time stayed right. Any ideas? Thanks! Hello everyone, here is my problem. I am working for someone that would like users to be able to create a Trial Account on their website. This trial account is to be used for 24 hours, once the 24 hours is completed they can no longer login/use that account. Everything about the Trial Account is completed, other then the account timer. I am at a loss on how to get this part done. I was thinking that I would use the MySQL commands, GETDATE() and DATEADD(), to get the date and time that the account was created and using the DATEADD() command I would add 24 hours to the GETDATE() value. Then if the user logs into at a date past the DATEADD() value the Users Account type is switched to 3(This means that the account has been disabled) and then is directed to the Logout page, which then redirects him back to the homepage. After this point, the user can no longer login due to the code preventing any user with an account type of 3 to login. The problem is that I am unaware how to incorporate the GETDATE() and DATEADD() functions into my PHP code. Would anyone be able to explain how I can get this done, or suggest a better method of inputting a starting time and ending time for the account? Thank You for any/all help. Hi peps, I am trying to write a script that allows for events to be executed at any given time that user gives it like if the user wants it at 2 am the event will execute at that given time i read some answer they say use cron but im using windows how to do it ?? |