PHP - About Timestamps
Hello,
input: I have a timestamp 1309438800 at GMT 1pm (2011-06-30 13:00:00 PM) output: how to make this timestamp at 1pm PST? cheers! Similar TutorialsPLEASE NOTE: I did check the PHP manual, although it was a bit too confusing for beginners with timestamps. I'm creating a simple bank interest system for my PHP game. Say I have a 2% interest in my bank, and I want to run it every 20 minutes. How would I do this? Please provide an example as I'm a bit...too lost. :/ Hi, I have 2 db fields with timestamp datatype (ClockingInDate and ClockingOutDate) and i am trying to get the difference between them then update the new db called duration with float datatype field. PHP: if (isset($_POST["clockout"])){ $result3=mysqli_query($con, "select * from attendance_records where OracleID='$session_id'")or die('Error In Session'); $row3=mysqli_fetch_array($result3); $end_date = $row3['ClockingOutDate']; $startdate = $row3['ClockingInDate']; $diff = strtotime($end_date) - strtotime($startdate); $fullDays = floor($diff/(60*60*24)); $fullHours = floor(($diff-($fullDays*60*60*24))/(60*60)); $fullMinutes = floor(($diff-($fullDays*60*60*24)-($fullHours*60*60))/60); $duration = $fullMinutes; $query3=mysqli_query($con, "update attendance_records set Duration = '$duration' where OracleID='$session_id' and isdone='$isdone'")or die('Error In Session'); header('location:index.php'); }
* come to think of that again, i always get zero, is it because it never reaches days in my program! I am using this for attendance system so only hours and minutes are used. Please help. Edited March 13, 2020 by ramiwahdanthoughts ok so i am using relative timestamps on my forum. if a post was created less than 1 hour ago then it will display something like 32 minutes ago. If it was posted more than 1 hour ago it will display "posted today at 10:00pm", or if it was older than 1 day then it will display the absolute time stamp. My question is how do i get them to update automatically like facebook does? i would like to use jquery for this but after searching i cant make heads or tales of any of it. so to sumarise i would like the timestamps to update without having to refresh the page. Thanks Why doesn't this code work... echo '<p>time() = ' . time() . ' seconds</p>'; echo '<p>$lastActivity = ' . $lastActivity . '</p>'; echo '<p>Seconds Active: ' . time() - $lastActivity . ' seconds</p>'; ...where $lastActivity comes from my database. When I run my script I see... Quote time() = 1331187131 seconds $lastActivity = 1331186745 -1331186745 seconds The last line is dropping the label and there is now Date Math?! Debbie Hi, Hope this is the right place for this... I have php routines that print itineraries. For some reason the timestamp (1351378800) returns the same date Sunday 28th October (2012) as the time stamp (1351465200). $count = 0; while ($count <= ($nights+1)) { echo date ('l', $startdate1)." (".$startdate1.")"; echo "<br>"; echo date ('jS F', $startdate1); $startdate1 += 86400; // more code } Any ideas? Many thanks, Peter 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? Ok sorry if this is a proper newbie question but say i have a timestamp saved in a database in a table called "news" and the field is called "date" how do i get the month and year only from example if the time stamp is 2011-03-29 13:57:05. And i want to list all news articles from the month 03 and the year 2011 how would i go about doing this? Thank you for reading hi, there. I have a timestamp in my mysql database called timestamp and it'll record the current timestamp. however it also records the time etc how do i use php to take apart these for example if i wanted to echo the date and time in a different order or if i only wanted to echo the date not the time. how is this possible? Thanks for reading. Getting the difference in seconds between two timestamps is easy. Taking the seconds and doing a breakdown of time frames is also easy. Code: [Select] <?php echo '<p>There are a total of ' . round($seconds) . ' seconds between Timestamp 1 and Timestamp 2.</p>'; echo '<p>There are a total of ' . round($seconds / 60) . ' minutes between Timestamp 1 and Timestamp 2.</p>'; echo '<p>There are a total of ' . round(($seconds / 60) / 1440) . ' days between Timestamp 1 and Timestamp 2.</p>'; echo '<p>There are a total of ' . round((($seconds / 60) / 1440) / 7) . ' weeks between Timestamp 1 and Timestamp 2.</p>'; echo '<p>There are a total of ' . round((((($seconds / 60) / 1440) / 7) / 30)) . ' months between Timestamp 1 and Timestamp 2.</p>'; echo '<p>The total breakdown of time from Timestamp 1 to Timestamp 2 is .</p>'; ?> What I am trying to figure out, is get a collective amount as well.... X years X months X weeks X days X minutes and X seconds. Does anyone have any good algorithm for handling that, or have any feedback on where to start to handle this type of math. Hi, I just want to see what way you guys think is best. On this little community I'm building I have decided to implement a function to see who were active within the last 15 minutes. I made a table (just user and timestamp) to register the last activity of any logged on user. Then I have a variable to take off 15 minutes from that but I can't get them to compare. Googling the issue I found people are solving this in very different ways. I wanted to see what phpfreaks recommend as the next step. Here is some code (that doesn't work properly - no results found as I compare to different timestamps): Code: [Select] include_once'header.php'; $now=time(); $now=(date("Y-m-d H:i:s")); //$mins = mktime(0,$now-15,0, date("Y"), date("m"),date("d")); $mins = time(); $mins15 = $mins-(60*15); $mins15 = (date("Y-m-d H:i:s", $mins15)); $online="SELECT * FROM user_online" WHERE last_activity > mins15; $result = mysql_query($online); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); echo <<< _END <div id='statusbar'> <h4>Online now: $rows</h4> Okay guys. My brain has completely froze and I can't figure out how to do this. I have one date, that is somewhat static it doesn't change regularly but it changes. Anyway let's say this timestamp is: 2011-07-12 10:30:00 and the current timestamp is 2011-07-13 10:30:00 That's exactly a day difference, which will produce 86400 seconds. How would I write this in PHP? I have an array that looks like this:
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "2021-02-10 09:06:18" [7]=> string(19) "2021-02-10 09:06:24" }
I need to group the instances any time there is more than a 6 second gap between elements. So 0 =>5 would be one array and 6 and 7 would be a new array.
Ive been able to produce the following but its not ideal
array(5642) { [0]=> string(19) "2021-02-10 09:04:48" [1]=> string(19) "2021-02-10 09:04:54" [2]=> string(19) "2021-02-10 09:05:00" [3]=> string(19) "2021-02-10 09:05:06" [4]=> string(19) "2021-02-10 09:05:12" [5]=> string(19) "2021-02-10 09:05:18" [6]=> string(19) "end" [7]=> string(19) "2021-02-10 09:06:18" [8]=> string(19) "2021-02-10 09:06:24" }
$burner_time = array(); foreach($Burner_Control_Alm as $new_burner){ $burner_time[] = strtotime($new_burner); } $repl = 'end'; for ($i=1; $i<count($burner_time); $i++) { $value_second = $burner_time[$i]; $value_first = $burner_time[$i-1] === $repl ? $burner_time[$i-2] : $burner_time[$i-1]; if ($value_second > $value_first + 6) { array_splice($burner_time, $i++, 0, $repl); } print_r($burner_time); } I have some data where each has a timestamp when is was measured, and not all data points will be necessarily measured at the same time.
Time | Value1 | Value2 | Value3 | Value4 2020-12-15 00:05:07 | | 27.6 | | 2020-12-15 00:30:27 | | | 26 | 2020-12-15 01:20:10 | 53.9 | 25.2 | | 2020-12-15 02:09:44 | | 26.4 | | 60.2 2020-12-15 02:43:19 | 33 | 49.2 | | 2020-12-15 03:04:44 | 30.8 | | | 42 2020-12-15 03:21:46 | | 54 | | 2020-12-15 03:47:19 | 52.8 | | 53.3 | 2020-12-15 04:26:20 | | | 27.3 | 2020-12-15 04:45:10 | 37.4 | | | 2020-12-15 05:24:25 | | 42 | | 2020-12-15 05:34:49 | 36.3 | | 41.6 | 2020-12-15 05:36:04 | 24.2 | | | 2020-12-15 06:06:47 | 49.5 | | | 2020-12-15 07:00:07 | | | | 49 2020-12-15 07:59:17 | | 27.6 | | 2020-12-15 08:14:45 | | | 48.1 | 51.8 2020-12-15 08:26:36 | | | 53.3 | 2020-12-15 08:54:23 | 50.6 | | | 2020-12-15 09:46:53 | | | 58.5 | 43.4 2020-12-15 10:25:03 | | | | 36.4
I am trying to obtain the total value (i.e. value1 + value2 + value3 + value4) and since there is no common time interval, come up with an arbitrary one such as one hour. It doesn't need to be exact and one option is to linearize each sample between each of their individual timestamps, but ideally I can do a little better. It has been many, many years, and I recall that least squares regression used to be used for things like this, but I think I would need to the process the entire series for each which I expect would require many polynomials. I've never used R and it might be an option if necessary. Not sure if there is anything baked into PHP which would be great. Any ideas? Thanks Time |Total Value 2020-12-15 00:00:00 | 27.3 2020-12-15 01:00:00 | 48.4 2020-12-15 02:00:00 | 21 2020-12-15 03:00:00 | 22 2020-12-15 04:00:00 | 49 2020-12-15 05:00:00 | 38 2020-12-15 06:00:00 | 49 2020-12-15 07:00:00 | 29 2020-12-15 08:00:00 | 49 2020-12-15 09:00:00 | 46 2020-12-15 10:00:00 | 36
|