hassank1 Posted May 5, 2008 Share Posted May 5, 2008 I've a cloumn "date" in my db of type datetime .. I want to add 8 hours for all the rows in the column date in my table.. how can I do that ? thx Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/ Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 <?php function split_date ($dateTime) { $splitTime ['year'] = substr ($dateTime,0,4); $splitTime ['month'] = substr ($dateTime,5,2); $splitTime ['day'] = substr ($dateTime,8,2); $splitTime ['hour'] = substr ($dateTime,11,2); $splitTime ['minute'] = substr ($dateTime,14,2); $splitTime ['second'] = substr ($dateTime,17,2); return $splitTime; } $loop = 0; $query = "SELECT `date` FROM `table`"; $result = mysql_query ($query); while ($row = $mysql_fetch_array($result)) { $loop++; $dateTime = split_date ($row['date']); $dateTimeTemp = $dateTime['hour'] + 8; if ($dateTimeTemp > 24) { $dateTime['day']++; $temp = 24 - $dateTime['hour']; $dateTime['hour'] = 8 - $temp } else { $dateTime['hour'] = $dateTime['hour'] + 8; } $dateTimeNew = "$dateTime[year]-$dateTime[month]-$dateTime[day] $dateTime[hour]:$dateTime[minute]:$dateTime[second]"; $query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'"; mysql_query ($query); } ?> This should do the trick. You still need to fill in your table name.. I just put "table" there Edit, sry now it's correct Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533818 Share on other sites More sharing options...
revraz Posted May 5, 2008 Share Posted May 5, 2008 Can't you do just a simple + 8 hours to a UPDATE? Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533831 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 Can't you do just a simple + 8 hours to a UPDATE? I have no idea since it's no timestamp but an actual formated MySQL DATETIME. Mine will do the job no matter what. But I don't know for better alternatives. he can just copy/paste this. Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533832 Share on other sites More sharing options...
revraz Posted May 5, 2008 Share Posted May 5, 2008 MySQL supports addition of hours. Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533837 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 I had no idea, this is an alternative I did. But can you go more simple than that? <?php function split_date ($dateTime) { $splitTime ['year'] = substr ($dateTime,0,4); $splitTime ['month'] = substr ($dateTime,5,2); $splitTime ['day'] = substr ($dateTime,8,2); $splitTime ['hour'] = substr ($dateTime,11,2); $splitTime ['minute'] = substr ($dateTime,14,2); $splitTime ['second'] = substr ($dateTime,17,2); return $splitTime; } $loop = 0; $query = "SELECT `date` FROM `table`"; $result = mysql_query ($query); while ($row = $mysql_fetch_array($result)) { $loop++; $splitTime= split_date ($row['date']); $dateTimeNew = mktime ($splitTime['hour']+8, $splitTime['minute'], $splitTime['second'], $splitTime['month'], $splitTime['day'], $splitTime ['year']); $dateTimeNew = date ('Y-m-d H:i:s', $dateTimeNew); $query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'"; mysql_query ($query); } ?> Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533840 Share on other sites More sharing options...
hassank1 Posted May 5, 2008 Author Share Posted May 5, 2008 thanks that exactly what I want .. however I think it need some modification because what if day is 30 then +1 .. I should add +1 to months etc... right ?? one more thing plz .. to get the current date I am using $DateNow = date("Y-m-d H:i:s"); //date @ this moment however how can I get the current date + 8 hours ...??? thx revraz -> I've tried ur method it didn't work :S ... can u give me ur query to test it ! maybe I've done something wrong in my query.. Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533842 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 my 2nd one should do the trick, mktime() atomatically updates the date. So if you add 8 hours it will automatically alter everything to make it fit Yes the G should be a H in that one, my bad x_x Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533844 Share on other sites More sharing options...
hassank1 Posted May 5, 2008 Author Share Posted May 5, 2008 $dateNOW = mktime ($splitTime['hour']+; $dateNOW = date ('Y-m-d H:i:s', $dateNOW ); would this give me the current time + 8 hours ?? Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533846 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 No, don't do that... the $splitTime['hour'] will surpass 24 hours god I suck at coding in a browser, second! Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533849 Share on other sites More sharing options...
hassank1 Posted May 5, 2008 Author Share Posted May 5, 2008 OK .. but now how can I save into a variable the current time + 8 hours .. (I mean this time I don't get the value from a table .. rather I want to use the current time and add 8 hours to it ) <-- that's another question .. however it's kind of similiar .. thanks .. btw sorry because u r coding in a browser Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533853 Share on other sites More sharing options...
DarkWater Posted May 5, 2008 Share Posted May 5, 2008 $now = time(); $nowplus8 = strtotime("+8 hour", $now); Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533855 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 <?php // Splits the date function split_date ($dateTime) { $splitTime ['year'] = substr ($dateTime,0,4); $splitTime ['month'] = substr ($dateTime,5,2); $splitTime ['day'] = substr ($dateTime,8,2); $splitTime ['hour'] = substr ($dateTime,11,2); $splitTime ['minute'] = substr ($dateTime,14,2); $splitTime ['second'] = substr ($dateTime,17,2); return $splitTime; } // Set the loopcounter to 0 $loop = 0; // Get the MySQL data $query = "SELECT `date` FROM `table`"; $result = mysql_query ($query); // Loop the rows while ($row = $mysql_fetch_array($result)) { // Increment the loop variable every loop $loop++; // Get the splitted time array $splitTime = split_date ($row['date']); // Make new time (adds the 8 hours) $dateTimeNew = mktime ($splitTime['hour']+8, $splitTime['minute'], $splitTime['second'], $splitTime['month'], $splitTime['day'], $splitTime['year']); // Format the timstamp into a MySQL DATETIME format $dateTimeNew = date ('Y-m-d H:i:s', $dateTimeNew); // Update the row $query = "UPDATE `table` SET date='$dateTimeNew' WHERE id='$loop'"; mysql_query ($query); } ?> I added some comments, this one should work though and for the current date that's fairly simple <?php $dateNew = mktime (date ('H')+8, date ('i'), date ('s'), date ('m'), date ('d'), date ('Y')); $dateNew = date ('Y-m-d H:i:s', $dateNew) ; ?> Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533859 Share on other sites More sharing options...
marcusfaye87 Posted May 5, 2008 Share Posted May 5, 2008 $now = time(); $nowplus8 = strtotime("+8 hour", $now); time () creates a timestamp, but he needs it formatted to DATETIME too which is Y-m-d H:i:s Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533871 Share on other sites More sharing options...
DarkWater Posted May 5, 2008 Share Posted May 5, 2008 Didn't know he needed DATETIME format. $now = time(); $nowplus8 = date("Y-m-d H:i:s", strtotime("+8 hour", $now)); Have a blast. Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533872 Share on other sites More sharing options...
PFMaBiSmAd Posted May 5, 2008 Share Posted May 5, 2008 You can do this directly in a query using the mysql date_add() function - http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-add You can also use the current date or datetime directly in a query using CURDATE() for just the date or NOW() for a datetime. Link to comment https://forums.phpfreaks.com/topic/104272-query-to-change-datetime-in-php/#findComment-533878 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.