PHP - Timestamp Vs Timezones
hey guys I wanting some information on the best way to store a timestamp and how I would convert that timestamp to another timezone please?
im guessing I would firstly store all timestamps to the database in GMT OR BST then convert after according to users timezone preference?
Is there a good website for this please on how this is possible...thank you
Similar TutorialsHello, I am a bit confused on how I want to go about developing my application. I see PHP is now moving towards dates using a Country/City format of dates. My question is how is everyone going about making simple timezone selectors (Like selecting the date/time on Windows) on their web applications? I really don't want to display a list of every city from America as there are a ton just in that category. Then I thought, we can calculate the time using an offset... Well php doesn't really have a function to set the time via an integral offset. So my question is... What is the best way to take a stab at this? The only way I can think of this is to make a list of the timezones, have an offset such as +1, +2, +3.... and then have an array that links the offset to the Country/City date format and then use that to set the default timezone lol Thanks in advance! Owner im setting up timezones so every user will be able to have the times displayed in their timezone. however when they register their timezone is sent to the database as an integer value: eg/ -3. meaning -3 hours from GMT. but to set the timezone in php i am using: date_default_timezone_set(); i have the list of available timezones but they just display the names and not the hour difference. Is there any where to get such a list or should i be doing things differently? hey guys trying to think of a practical way for a user to select his/her timezone without having to listing all in a select element like so:
Europe/London, Europe/Luxembourg, Asia/Singapore etc...
i was thinking of having a select element listing +00:00, +01:00 etc... but i would have the problem of DST when clocks change
what is the best solution for this please?
thank you
So the most relevant thread I found was here, I just want to get a little more information. In an application I'm developing, a user may decide to change their timezone. Currently I'm setting my timezone to GMT with date_default_timezone_set(). However I just realized that when inserting date into MySQL DATETIME fields it is (quite obviously) using the server time. So what is the best method here. Use SET GLOBAL time_zone = timezone; as indicated in the MySQL manual. Change my field types to TIMESTAMP instead, from what I've read it converts it to UTC (which in essence is GMT) before storing in the database. After testing this though, it still inserts NOW() as the servers date/time. So not sure what the go is there. Store a UNIX timestamp in the database, and convert it before display with PHP. I'm at a loss here as to what would be the cleanest, and most suitable solution for an application that depends heavily on the timezone so it can accurately display dates and time. Cheers. As you may of guessed, it's a nightmare. If my birthday is on 10th June GMT, it would still be my birthday on the 11th June in Australian time. Though.. the 11th June, is not my birthday. It's not hard to understand this bit as Australia is like.. many hours ahead. Though when it comes to PHP... if I used some Timezone plug-in then this would bring up the same result as it does now. Though when that persons age is displayed... if my birthday is 10th June at 1AM, in America, it would be the 9th June. There for, they won't see my age change to 18 until it is 10th June their time. To prevent this, is when calculating the persons age, you would be adding their timezone offset onto their birthday timestamp. So regardless on what day it is around the world, if they are celebrating their birthday... then it's their birthday all around the world even though it's different times. To do this, i would need timezone offsets. I would have a field in the database I assume. I would convert their DOB to timestamp. I would add their offset onto that timestamp or take it away and display whatever result that gives to every user. This would display their age as whatever to every user. The next problem is... strtotime works in MM/DD/YYYY, so how do I make it work with DD/MM/YYYY ? I'm actually not making much sense, how would you work with Timestamps and Ages? Also for my next question question, if I had someones DOB, how do I bring up their next birthday out of it for when I am displaying "Bobs birthday is on the..." Now for final, apparently on 19th Jan 2038 the UNIX Timestamp will cease to work due to a 32-bit overflow. How can this be true when converting a date in the year 2500 still brings a timestamp? Hi, I have in my DB a column with a timestamp as definition. Now I would like to have only the month and year of that column for further use in a form. How can I achieve that? Thanks! I have this date in database I must convert it to Unix Timestamp How ? 2010-11-11 09:04:44 I try this echo mktime($mydate); but its not work its return wrong Timestamp becase if I convert back it I have wrong date 2011-02-16 21:01:33 How do i correctly update a timestamp? Code: [Select] $date = date("Y-m-d H:i:s"); mysql_query("UPDATE mmembers set time=now() where email=$myusername"); What is the proper way to turn a date that is in "m/d/Y" format, into a correct timestamp. When I try strtotime, then try to change it back to a date, it shows up incorrectly (defaulting to the 1969 date that it does when it messes up. I just need to be able to turn $date = '10/15/2011'; to and from a timestamp correctly. Any advice? Hey there, Thanks for taking the time to read my thread. I've encountered a problem that I cannot figure out a way to solve. I need to calculate the remaining days until a specified unix timestamp. I haven't yet figured out how to get the amount of days until the timestamp. If anybody could tell me how to get the remaining days until a unix timestamp I would be grateful, thanks. I have a update query that should be updating the timestamp when a "month" field is changed. I have the following code:
$month_update->bindParam(':timestamp', strtotime (date("Y-m-d H:i:s")), PDO::PARAM_INT);
The result that is returned is 1403198033. I need it to look like this 2014-03-26 07:09:06. I know I am missing something and I would greatly appreciate your help.
Thank you so much!
$epocTime = $math.newInt($math.divide("$today.getTime()", "1000"))) I need to set two variables for use within my pages, one containing a date as a string in format 'YYYY-MM-DD HH-MM-SS' and another containing the same date as a timestamp. The date and time stored will take on the value passed in the url if available, else will be for the current date. The time should always be midnight at the beginning of the date. eg. $datestr = '2010-09-08 00:00:00' and $datestamp = '1283904000' Where a date is passed in the url, it will be in the format 'YYYY-MM-DD'. The problem I am having is getting an accurate timestamp. I think the trouble is connected to the handling of daylight saving time as it it always an hour out. What I have at the present is as below: Code: [Select] <?php // ensure all date and time functions are relative to the correct timezone (UK) date_default_timezone_set('Europe/London'); // string holding selected / current date in format 'YYYY-MM-DD HH:MM:SS' $datestr = (isset($_GET['date']) ? $_GET['date']." 00:00:00" : gmdate("Y-m-d")." 00:00:00"); // int holding selected / current date as unix timestamp $datestamp = strtotime($datestr); ?> Can anybody give me a simple way of converting $datestr into $datestamp to ensure that this is always correct for my UK timezone, irrespective of whether we are in or out of daylight saving time? Hey there, Thanks for taking the time to read my thread. My issue is if I'm given a time stamp in PHP how could I calculate the number of days until that time stamp?. Thanks for your time. Hi am trying to add date and timestamp of when a comment gets posted.Am not gr8t at php but still learning. If any one can help me on this would be reil gr8t. Sry for the long post. As for now i have this Code: [Select] $date = date("M j, y, g:i a"); and echo's out the date, time [/code]$date[/code] as in the current date and time which i dont want. Code: [Select] <?php $query = yasDB_select("SELECT * FROM newsblog"); if($query->num_rows == 0) { echo '<div id="newsblog_text">This news blog has no comments, become a member and be the first to add one!</div>'; } else { $query = yasDB_select("SELECT * FROM newsblog WHERE userid = '$id'"); while($row = $query->fetch_array(MYSQLI_ASSOC)) { $date = date("M j, y, g:i a"); $text = $row['comment']; $text = str_replace(':D','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/biggrin.gif" title="biggrin" alt="biggrin" />',$text); $text = str_replace(':?','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/confused.gif" title="confused" alt="confused" />',$text); $text = str_replace('8)','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cool.gif" title="cool" alt="cool" />',$text); $text = str_replace(':cry:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/cry.gif" title="cry" alt="cry" />',$text); $text = str_replace(':shock:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/eek.gif" title="eek" alt="eek" />',$text); $text = str_replace(':evil:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/evil.gif" title="evil" alt="evil" />',$text); $text = str_replace(':lol:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/lol.gif" title="lol" alt="lol" />',$text); $text = str_replace(':x','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/mad.gif" title="mad" alt="mad" />',$text); $text = str_replace(':P','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/razz.gif" title="razz" alt="razz" />',$text); $text = str_replace(':oops:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/redface.gif" title="redface" alt="redface" />',$text); $text = str_replace(':roll:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/rolleyes.gif" title="rolleyes" alt="rolleyes" />',$text); $text = str_replace(':(','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/sad.gif" title="sad" alt="sad" />',$text); $text = str_replace(':)','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/smile.gif" title="smile" alt="smile" />',$text); $text = str_replace(':o','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/surprised.gif" title="surprised" alt="surprised" />',$text); $text = str_replace(':twisted:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/twisted.gif" title="twisted" alt="twisted" />',$text); $text = str_replace(':wink:','<img src="' . $siteurl . 'templates/' . $theme . '/styles/images/smileys/wink.gif" title="wink" alt="wink" />',$text); echo '<div class="newsblog_box1"><div class="newsblog_name">' . $row['name'] . ' '.$date.'</div></div> <div class="newsblog_box2">' . $text . '</div>'; } }?> I have a timestamp field in my mySQL table and need to check if it's 14 day or older. What's the best way to do this? Any help will be appreciated Hello! I'm trying to develop some kind of RSS news aggregator and I want to show only feeds younger than 1day. I figured i could transform RSS pubDate to timestamp (strtotime()), but there are some feeds without timestamp (like: http://izklop.com/xmldata/rsslinks.xml). Is there any other way to do it, so I could find timestamp from those feeds without pubdate? If there isn't any other way, do You think it is ok, to just show last 5 feeds? I hope I made my self clear, and please forgive me for my English Hi. I have this code which input data to the database. Code: [Select] <?php include("connect.php"); echo $date = date("Y-m-d H:i:s"); echo "\n <br />"; echo $date2 = date('Y-m-d H:i:s', strtotime("+20 seconds")); mysql_query("UPDATE users2 SET date2 = '$date2'"); $query = mysql_query("SELECT date2 FROM users2"); $numrows = mysql_num_rows($query); if ($numrows != 0) { while ($row = $mysql_fetch_array($query)) { $date_final = $row['date2']; } } echo $date_final; ?> Variable date2 is stored as timestamp. Now i want to display this two variable on the web, but this code doesn't work. Why? Thank you. I've been looking at converting my php timestamps to display as month, day, and the hour and minute the event occurred on, i tried; Code: [Select] $time = date("j/n H:i", $Events['timestamp']); But no success can anyone point me in the correct direction? Thanks How would I go about adding a timestamp to my image uploads so I don't erase image files with the same name? Also, what would be the correct use of the mysql_escape_string <?php require_once('storescripts/connect.php'); mysql_select_db($database_phpimage,$phpimage); $uploadDir = 'upload/'; if(isset($_POST['upload' . $config])) { foreach ($_FILES as $file) { $fileName = $file['name']; $tmpName = $file['tmp_name']; $fileSize = $file['size']; $fileType = $file['type']; if($fileName==""){ $filePath = 'upload/'; } else{ $filePath = $uploadDir . $fileName; } $filePath = str_replace(" ", "_", $filePath); $result = move_uploaded_file($tmpName, $filePath); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $fileinsert[]=$filePath; } } $mid = mysql_real_escape_string(trim($_POST['mid'])); $cat = mysql_real_escape_string(trim($_POST['cat'])); $item = mysql_real_escape_string(trim($_POST['item'])); $price = mysql_real_escape_string(trim($_POST['price'])); $about = mysql_real_escape_string(trim($_POST['about'])); $fields = array(); $values = array(); $updateVals = array(); for($i=1; $i<=4; $i++) { $fields[$i] = 'name'.$i; $values[$i] = mysql_real_escape_string(basename(trim($_FILES[$fields[$i]]['name']))); if($values[$i] != '') { $updateVals[] = "{$fields[$i]} = '{$values[$i]}'"; } } $updateNames = ''; if(count($updateVals)) { $updateNames = ", " . implode(', ', $updateVals); } $update = "INSERT INTO image (mid, cid, item, price, about, name1, name2, name3, name4) VALUES ('$mid', '$cat', '$item', '$price', '$about', '$values[1]', '$values[2]', '$values[3]', '$values[4]') ON DUPLICATE KEY UPDATE cid = '$cat', item = '$item', price = '$price', about = '$about' $updateNames"; $result = mysql_query($update) or die (mysql_error()); $id = mysql_insert_id(); ?> <p style="font-size:35px; font-family:Arial, Helvetica, sans-serif; color:#255E67; margin-left:25px;">Your Item Has Been Uploaded!</p> <script type="text/javascript"> setTimeout('ourRedirect()', 2000) function ourRedirect() { location.href='protator_php.php?mid=<?php echo $id ?>' } </script> |