PHP - Date And Time Issues
I'm having some trouble understanding how to use date and time when updating my database.
The table is already populated with Date and Time fields in date and time format. I need to update the table based on the date and time, but I can't make it work. I tested all the variables and they are being sent correctly. I'm pretty sure the problem lies in how I'm trying to match the Date and Time fields with $dateID and $timeID in the WHERE part of the query, but I'm not sure how to fix that! Code: [Select] <?php include("opendatabase.php"); $size = count($_POST['player1']); $i=0; $thisyear = date("Y"); while ($i < $size) { $dateID = $_POST['date'][$i]; $timeID = $_POST['time'][$i]; $player1 = ($_POST['player1'][$i]); $player2 = ($_POST['player2'][$i]); $player3 = ($_POST['player3'][$i]); $player4 = ($_POST['player4'][$i]); $player5 = ($_POST['player5'][$i]); mysql_query(" UPDATE Daysheets$thisyear SET Player1='$player1', Player2='$player2', Player3='$player3', Player4='$player4', Player5='$player5' WHERE Date = '$dateID' AND Time = '$timeID' "); $i++; } header("Location: /teetimes/publicteetimes.php?teetimedate=$dateID"); mysql_close($con) ?>Any help appreciated! I should also mention that whenever I run this query it erases anything that was previously entered in the database except the date and time columns... Similar TutorialsI'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. 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. 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 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! Hey I'm having issues with using strtotime(), date(), and time(). I have a couple problems. The first is: I used strtotime to get the unix of a specific date and time (worked great, even outputted the correct time using date() to return it). However when I went to post information on it (I set it up so that you can't post after the event started), it told me it already started like an hour before it was supposed to. I used Code: [Select] if (time() > $event['when']) { echo "too late."; } else { do it } to determine. That had me very confused. Then I decided instead of manually inputting the event times I would pull them from an xml file, which I had no trouble reading. I used: Code: [Select] strtotime("Next $day, $time PM") with $day = Sun, $time = 1:00- This outputted a unix timestamp but then date() returned it as being 7 pm on sunday instead. Any ideas what would be causing these strange things? I have a batch script that reads from a timestamp.txt file to determine the last time it ran and I need to modify that by two hours. What happens is the script will run to look for changes in the county records and their server time is two hours different from mine. Code: [Select] $fr=fopen("xml/timestamp.txt",'r'); $mydate=fread($fr, 50); if ($incremental_updates == 1){ $default_template_file="xml/incremental_template.xml"; $default_config_name="xml/incremental.xml"; $contest=file_get_contents($default_template_file); $contest=str_replace("{DATE_TAG}", $mydate, $contest); $f=fopen($default_config_name,'w+'); fwrite($f, $contest); fclose($f); fclose($fr); $fw=fopen("xml/timestamp.txt",'w'); fwrite($fw, substr(date('c'), 0, 19)); fclose($fw); }else{ $default_config_name = "xml/full_update.xml"; } I tried making a time stamp update look like $cur_timestamp=date("Y-m-d H:i:s", time() +14400); which is correct but it doesn't write to the text file properly. Using the above fwrite($fw, substr(date('c'), 0, 19)); I get the correct results in the timestamp.txt file for my server time. I need it to be +14400 2011-05-25T08:27:51 would essentially become 2011-05-25T10:27:51 when it writes to the timestamp.txt I hope that makes sense. J Hi! The below dropdown will show dates/times every 15 minutes from 12am to 11:45pm for only 1 day. I want it to show for 5 days, anyway I can do this? Below code: Code: [Select] $start = strtotime('12:00am'); $end = strtotime('11:45pm'); echo '<select name="time">'; for ($i = $start; $i <= $end; $i += 900) { echo '<option>' . date('F j, Y, \a\t g:i a', $i); } echo '</select>'; Thanks for any help. What is the best way to insert this time stamp into mysql? This is the info I want... Example: Sunday, 28.1.11, 9:56 How would I also select time from the database using mysql? Hey, I need help with date, I have stored dates in my table in this format: i.e. 22.10.2011 but now use timestamp to store dates. Problem is with this query the WHERE clause will find dates in the old format and I want to change that to the new format. Code: [Select] for($i = $count; $i > 0; $i--) { $day = date("d.m.Y", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y"))); $tmp = mysql_fetch_array(safe_query("SELECT count FROM ".PREFIX."counter_stats WHERE dates LIKE '%".$day."'")); $array[] = $tmp['count'] ? $tmp['count'] : 0; } Am I making sense ? I have a field set as datetime and want to compare that field to see if it is older then ten minutes. Can anyone help I am having a brain fart? Hey, I need help with my date & time string. I need to edit it do show in a string + also use it for calculating things like when the date has passed. The string is like this: Code: [Select] <?php $string = '2011-01-01 00:00 AM'; ?> So i wanted to check if there was a function that can automatically convert it, so it would say for example: Quote Sunday, 1st of January 2011 00:00 AM But at the same time compare $string against server time to see if the date has expired? Hope you can help I'm trying to write a function that'll give me the days of the week of any submitted date. This keep returning 1/30/2011 even though I am submitting 1/05/2011 (the Monday should be 1/03/2011)...I know it's incomplete I'm just trying to get the Monday right now Code: [Select] function getWeekDays($date) { //convert string to date $thisdate = date($date); $dow = idate('w', mktime(0,0,0,$thisdate)); if ($dow == 1) { $monday = date('m/d/Y', mktime(0,0,0, date('m'), date('d', $thisdate), date('Y'))); } else if ($dow == 2) { $monday = date('m/d/Y', mktime(0,0,0, date('m'), date('d', $thisdate) - 1, date('Y'))); } else if ($dow == 3) { $monday = date('m/d/Y', mktime(0,0,0, date('m'), date('d', $thisdate) - 2, date('Y'))); } else if ($dow == 4) { $monday = date('m/d/Y', mktime(0,0,0, date('m'), date('d', $thisdate) - 3, date('Y'))); } else if ($dow == 5) { $monday = date('m/d/Y', mktime(0,0,0, date('m'), date('d', $thisdate) - 4, date('Y'))); } $weekarray = array($thisdate); return $weekarray; } I just recalled using date(); time() for lots of things in my database, including post times etc. At first I simply assumed this used server time or something, but when I changed my PC's time, it changed ! Now, shortly after this I simply assumed this was because of me using a localhost? But I am not certain anymore.. If I would place this online, would date() and time() return you server time or user time? I couldnt find it on google =-( Is it possible to convert time(); to date();? Hey guys, What i would like to create is the ability to calculate the difference between two dates / times. For example: 2010-12-13 10:00:00 to 2010-12-17 17:00:00 In a format of days / hours and minutes / seconds. However, I only want to calculate for an 9-5 work day, not the hours outside. Is this possible? If so, how would I go about creating something like this. I'm looking to enter the current time and date into a mysql database using php. My current code looks like this: Code: [Select] $now = date("m/d/y",time()); I then insert $now into the table into a datetime column. This all works but the time when read from the column appears as 0000-00-00 00:00:00. Any ideas what I'm doing wrong? Thanks in advance. I am writing an app that takes ofx banking files files on convert them to csv for excel or importation into sql. I am on the last task. Converting the nonstandard date:time stamp. I am using preg_replace(). I am not the best at regular expressions. I am getting this error: "Warning: preg_replace() [function.preg-replace]: Unknown modifier '\' in C:\xampp\htdocs\banking\ofx-date.php on line 13" CODE: // Date Time 20110228000600 (2011/02/28/, 00:06:00) // Expected Return '<DTPOSTED>2011/02/28,00:06:00' $ofxDate = '<DTPOSTED>20110228000600'; $ofxNewDate = substr($ofxDate,0,10) . substr($ofxDate,10,4) . '/' . substr($ofxDate,14,2) . '/' . substr($ofxDate,16,2) . ',' . substr($ofxDate,18,2) . ':' . substr($ofxDate,20,2) . ':' . substr($ofxDate,22,2); echo htmlentities($ofxNewDate) . PHP_EOL;; $patternA = '/<DTPOSTED>/\b[0-9]+\b'; //$replacement = '/<DTPOSTED>/'; $stringB = preg_replace($patternA, $ofxNewDate, ofxDate); echo htmlentities($StringB) . PHP_EOL; Are time zones when calling the date function automatically decided based on where you live? Or is that something that I have to implement--Because the time is wrong according to my time zone. |