PHP - Brain Freeze > Finding Seconds Between 2 Timestamps.
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? Similar Tutorials
Hi, echo '<div style="float:right;width:60%;color:#666666">'. TeamSpeak3_Helper_Convert::version($ts3->virtualserver_uptime) .'</div>'; Edited June 24, 2020 by Lux05 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! PLEASE 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. :/ 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 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 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, 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 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. 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, 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> 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. I think this problem requires LOTS of brainpower, and as I am absolutely convinced at this point that I have none, I was kind of hoping someone could steer me in the right direction at the very least. What I'm scripting right now is a search form that allows users to browse members based on what they type in the forms. There's about 8-10 forms of various criteria, such as age, country, birthdate, email, etc. And once they hit submit, all the information they typed gets sent to the url ($_GET, in other words). Now, the way I structured my script is like this: Code: [Select] <?php //If nothing was submitted (or if the url is completely blank) if ($_GET == array ()) { //Define a variable that will be used to query the members; in this case, it would select all members $query = "SELECT * FROM fans"; } else { //If the user typed at least one thing (in the form OR the url) if (count ($_GET) == 1) { //If what they typed is only for one criteria, define a variable that creates a query for only ONE criteria to search for $query = "SELECT * FROM fans WHERE"; foreach ($_GET as $field => $value) { $query .= " $field = '$value'"; } //If the user has typed in more than one field and hits search } else { //Define a variable for a query that selects members based off each criteria $query = "SELECT * FROM fans WHERE"; foreach ($_GET as $field => $value) { $query .= " $field LIKE '%$value%' AND"; } } } ?> Now, the problem with the above script is the very last part...defining a query to run if more than one field is selected. Because I'm using a foreach loop to structure that query, my options seem rather limited with what I can and can't do as far as querying the database. And the problem lies in where it says "$field LIKE '%$value%' AND". Because of the way the script is set up, the last $_GET variable will have "AND" at the very end, and when it runs through the mysql_query() function, it breaks, and does NOT work. Is there a way around this? I figure it probably requires some complex mathematical equation (one which I have absolutely no idea on how to create). Can anyone help? For some reason I cannot think today. Code: [Select] <?php class { function meh() { $blah = $this->profile(); echo $blah->weirdname; } function profile() { // this is where I'm stuck.. // how do I properly set weirdname? } } ?> It's been a while since I've played with classes (been doing other things) so how do I set up the function profile() to return a value for weirdname. It's one of those days after a long weekend and a rainy morning. Simply trying to get the correct message depending on the variable's value if($a = 'one'){ echo "POOR";} if($a = 'two'){ echo "GOOD";} if($a = 'three'){ echo "VERY GOOD";} if($a = 'four'){ echo "EXCELLENT";} Not sure if I need to use ==, extra quotes, or ELSEIF for the best result.
Okay, I have a 3 column table layout on my forum, I want users to beable to select where they want 1 of there profile sections to go under, (and display order) either, left middle or Right side. See now I can code the javascript/etc ajax to make them update if selected to move to (any direction) But I need help to do it server side, what kind of value/row in my db should i use? just 1 field and use arrays? Also, let's say I want to move the "Calendar" to the middle or far right, and save that position, what kind of functions would i be working with (in php) I can prob do it if I use like 200 IF and ELSE conditions, but that would take way to long, any help? I am trying to create some navigation link with sublinks in a <ul> list. Here is the set up. Table = categories fields: id cattitle = title parentid = level of the link (0 = top level) cat_loc = the id value of the link that this link will be sublink of example: 1st link: id 1, title - home, parentid - 0, cat_loc - null; 2nd link: id - 5, title - info, parentid - 0, cat_loc - null; 3rd link: id - 6, title - test, parentid - 1, cat_loc - 5; so test should be a sublink of info. Here is what I have for code so far... <?php include $_SERVER['DOCUMENT_ROOT'] . './includes/magicquotes.inc.php'; include $_SERVER['DOCUMENT_ROOT'] . './includes/db.inc.php'; $result = mysqli_query($link, 'SELECT * From categories order by orderby asc'); if (!$result) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } while ($row = mysqli_fetch_array($result)) { $navlink[] = array('id' => $row['id'], 'cattitle' => $row['cattitle'], 'cat_loc' => $row['cat_loc'], 'parentid' => $row['parentid']); } echo '<ul>'; $count = 0; foreach ($navlink as $nav): { if ($nav['parentid'] == 0) { if ($count!=0){echo " | ";} echo '<li>' . '<a href="home.php?id=' . $nav['id'] . '"' . ' ' . 'title="' . $nav['cattitle'] . '">' . htmlspecialchars($nav['cattitle']) . '</a>'; $count++; } if ($nav['parentid'] != 0) { $result2 = mysqli_query($link, 'Select * From categories where "$nav[cat_loc]" = "$nav[id]"'); if (!$result2) { $error = 'Error getting categories: ' . mysqli_error($link); include 'error.php'; exit(); } echo '<ul>'; while ($row2 = mysqli_fetch_array($result2)) { $subnavlink[] = array('id' => $row2['id'], 'cattitle' => $row2['cattitle']); } foreach ($subnavlink as $subnav): { echo '<li>' . '<a href="home.php?id=' . $subnav['id'] . '"' . ' ' . 'title="' . $subnav['cattitle'] . '">' . htmlspecialchars($subnav['cattitle']) . '</a></li>'; } endforeach; } } endforeach; echo '</ul>'; yes I do have the php closed further down the page. Part of my problem is that the sublinks won't be in the database right after their parent links so I don't think the way I have my foreach loop set up is right. I know I don't have my <ul>'s and <li>'s closed correctly yet. I just want to get the subnavs to show up, I can fix those later. Any ideas? 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
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); } hey peeps, I am trying to get my site to display related youtube videos under the main video here is what i have $query=mysql_query("SELECT title,mp3ID,youtube_ID FROM songs ORDER BY RAND() LIMIT 10 ") or die("Error Occured"); $left = true; while($row=mysql_fetch_array($query)) { if ($left) { echo "<tr>"; } echo "<td align='justify' width='100%' cellspacing='2' cellpadding='2' >"; echo "<td><img src='http://img.youtube.com/vi/".$row['youtube_ID']."/default.jpg'></td>"; echo "<td nowrap><a href='/music.php?id=".$row['mp3ID']."'>".$row['title']."</a></td>"; echo"</td>"; if (!$left) { echo"</tr>"; } $left = !$left; } ?> this is basic info of how i am able to get youtube video titles and image....i want to use the same youtube.com video ID To get related videos please help!! |