PHP - Datetime Difference
Basically I am using DATETIME to store the time in which a row was input.
Format - 2010-11-13 21:31:53 How would I get the current time of page load and compare it to the database row "2010-11-13 21:31:53" then output the difference. Example $sqlresult = 2010-11-13 21:31:53; $currentTime = (GET CURRENT DATE+TIME = (2010-11-13 21:31:55) ); Output - This was posted 2 minute's ago. Thank you in advance. Any help would be greatly appreciated. Similar TutorialsDear all, how can I convert a string "yyyy-mm-dd hh:mm" into a really datetime value with php? thanks! Hello All, I have the following code: Code: [Select] echo "<table width='615px' cellpadding='3' cellspacing='4' align='center'>"; echo "<tr>"; echo "<td align='left' width='150px'>"; echo substr($row['date'],0,11); echo "</td>"; echo "<td align='left' width='465px'>"; echo $row["food"] ; echo "</td>"; echo "</tr>"; echo "</table>"; And this outputs: Apr 4 2011 Chicken Ceasar Salad, Garlic Toast, Cloddhoppers, Applesauce, & Milk (etc.) How can I add "Mon" in front of "Apr 4 2011"? thanks! This code gives me 1969-12-31 18:33:30 Code: [Select] <?=date('d M Y', $val['Created']);?> and this one gives me 2010-11-04 03:52:54 WHICH IS THE RIGHT ONE - BUT i need to format as above. Code: [Select] <?=$val['Created']?> this happens on the same line of code. What is wrong? Hey, does anybody know a good function, where I can have the trendy "time ago" functionality?
A good tutorial would be cool, too.
I would be using the datetime function off MySQL.
Hi, I'm a newbie here, and with PHP, and need some help with the datetime display. I am wanting to utilize old Naval comms DTG (datetimegroup) format which is DDHHMM 3-digit Month 2-digit year. The DDHHMM = Day (01 - 31) HH (hour 01 - 24) MM (minute 00 - 59) What I need to do is to add the letter Z (meaning GMT, ZULU) like thus DDHHMMZ FEB 11 How can I go about inserting that Z into this? Tks in advance for any assist Hi Everyone, Please have a look at this, and tell me if its right? I am generating a sitemap for my website dynamically. The date I am providing: 2012-02-24 11:56:09 and this is the output I am getting: 2012-02-24T11:56:09Z now I am not sure of the Z at the end of the returned value. I think I am looking for something similar to this: 2012-03-12T12:00:02+02:00 not to sure what I am doing wrong and where it gets the timezone from, I am guessing from the php config file. any help would be much appreciated. thanks My code: Code: [Select] <lastmod><?php echo $time->toAtom($post['Post']['modified']); ?></lastmod> Hello!! i want to save datetimes into a mysql database. Their format is dd-mm-yy hh:mm. I think mysql format for datetime is yy-mm-dd hh:mm..How can i convert them?? I'm given something like this: Sun Nov 06 2011 09:30:00 GMT-0500 (EST) And i'm trying to turn that into 2011-11-06 09:30:00 I started by using explode and trying to loop through the day and month to figure it out, but that seems inefficient and overly complicated. Am i missing something? Thanks Code: [Select] OFXAdapter: Failed to parse request: Unable to parse an atomic field "OFXRequest.OFXRequest.SignOnMsgsRqUn.SignOnMsgsRqV1.SonRq.DtClient": field value "20101005091544.000[-4:EDT]" is not a valid (YYYYMMDDHHMMSS.XXX[gmt offset:tz name]) date. I am trying to put together some PHP code to use the OFX standard to communicate with my bank in order to download my recent transactions. The standard details that in the sign-on request, the date and time must be included in the format listed above. As you can see, I have the value in the exact same format as specified. So my question is this: is there something about GMT offset or TZ names that I am missing here? Hello, I am trying create a news article, but I have few category in it. Can you help me to create an "add article form" which has dropdown list of category that I can choose, so when I select "Announcement" it will save the data on my mysql database under announcement table. sample html form below. <form id="form1" name="form1" method="post" action=""> <label> Category : <select name="category" id="category"> <option>--- choose category ---</option> <option>News</option> <option>Announcement</option> <option>Sports</option> </select> </label> <br /> Date: <br /> Title: <label> <input type="text" name="title" id="title" /> </label> <br /> Body: <label> <textarea name="body" id="body" cols="45" rows="5"></textarea> </label> <br /> <label> <input type="submit" name="Submit" id="Submit" value="Submit" /> </label> <br /> <br /> </form> Can you tell me how to create the php code which support the above html. Also, I would like to have a date and time on my article, but I want it automatically get the time and date from the computer of the client, so the user will not put it manually. can you point me to the correct code? Thanks and hope you can help me! I have a function that receives two datetime values attached to the variables $start and $end. These will be in the format 'Y-m-d H:i:s'. For each of these values, I create a DateTime object to make the handling of them easier: Code: [Select] $startDT = DateTime::createFromFormat('Y-m-d H:i:s', $start); $endDT = DateTime::createFromFormat('Y-m-d H:i:s', $end); What I need to do now is ensure that the seconds of the $startDT value are always set to '00' and the seconds of the $endDT value are always set to '59'. Is there an easy way to do this without having to extract the individual values for hours, minutes and seconds before reapplying them using the setTime function, adjusting the values accordingly? I have multiple datetimes. The format is dd-mm-yy, hh:mm. I want to insert it into a table using mysql. However mysql format is yy-mm-dd, hh:mm:ss. How can i do it and what type should i use (datetime?timestamp?). Thanks for your help!! I have a varaiable which gets the server time; $date =date("Y-m-d H:i:s"); suppose the server is in the USA then it the timezone of this date will be the timezone of USA. And i want to convert this timezone to Asia/Karachi timezone. Any Idea about this??? I have a page where "Events" which may be of interest to site visitors can be recorded. Among other info, an "Event" has a "from" date and a "to" date (to show that an event will run from October 29th to October 31st, for example). All event info is being written to the db, but the "from" and "to" dates are not. (In the db, "0000-00-00 00:00:00" is being entered in the "from" and "to" columns.) In the Events table, both event_date and event_end_date are of type datetime. Dates are entered into the form fields via a javascript datepicker and are in the format "24/09/2011". I'm using this code: Code: [Select] // Check for a Start Date: if (empty($_POST['from'])) { $errors[] = 'You forgot to enter a Start Date for the event.'; } else { $from = $_POST['from']; } // Check for an End Date: if (empty($_POST['to'])) { $errors[] = 'You forgot to enter an End Date for the event.'; } else { $to = $_POST['to']; } The db query is this: Code: [Select] if (empty($errors)) { // If everything's OK. // Insert the Event info in the database... // Make the query: $q = "INSERT INTO events (title, event_date, event_end_date, venue, ext_link, blurb) VALUES ('$title', '$from', '$to', '$venue', '$ext_link', '$blurb') "; $r = @mysqli_query ($dbc, $q); // Run the query. if ($r) { // If it ran OK. // Print a message: echo "<h2>Event info added!</h2> <h3>Your entry is now visible on the <a href='index.php'>Home Page</a>.</h3> <p><a href='logout.php'><strong>Log out</strong></a></p> " ; mysqli_close($dbc); include('inc/footer.php'); exit(); And the form is this: Code: [Select] <form id="form1" name="form1" method="post" action="add_event.php"> <label for="title">Title</label> <input type="text" name="title" id="title" value="<?php if (isset($_POST['title'])) echo $_POST['title']; ?>" /> <script> $(function() { var dates = $( "#from, #to" ).datepicker({ defaultDate: "", changeMonth: true, numberOfMonths: 1, onSelect: function( selectedDate ) { var option = this.id == "from" ? "minDate" : "maxDate", instance = $( this ).data( "datepicker" ), date = $.datepicker.parseDate( instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings ); dates.not( this ).datepicker( "option", option, date ); } }); }); </script> <label for="from" >*From</label> <input type="text" name="from" id="from" value="<?php if (isset($_POST['from'])) echo $_POST['from']; ?>" /> <label for="to" ">*To</label> <input type="text" name="to" id="to" value="<?php if (isset($_POST['to'])) echo $_POST['to']; ?>" /> <label for="venue">*Venue</label> <input type="text" name="venue" id="venue" value="<?php if (isset($_POST['venue'])) echo $_POST['venue']; ?>" /> <label for="blurb">*Description</label> <textarea name="blurb" id="blurb" value="<?php if (isset($_POST['blurb'])) echo $_POST['blurb']; ?>" rows="5" ></textarea> <label for="ext_link">External link (optional, entered like "www.example.com")</label> <input type="text" name="ext_link" id="ext_link" value="<?php if (isset($_POST['ext_link'])) echo $_POST['ext_link']; ?>" /> <input type="submit" name="submit" value="Insert Event Info" class="submit" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> Can anyone tell me why the dates aren't being recorded? Any help will be greatly appreciated. Hi, basically i am parsing some rtf/word files and generating excel csv's for upload into mysql database....my issue is in one of the fields i want to store the value in the mysql datetime / date format but excel keeps reformating it. Any help on how to overcome this will be highly appreciated. Hi all, I have a loop that is modifying a DateTime object. It goes through X number of times and adds 1 day to the date time object and stores the modified object in an array. I use ->add(new DateInterval('P1D')); For some reason it is losing an hour each time that I add a day. I start out at 19:00 and each iteration through the loop adds a day, but loses an hour. 2010-09-19 19:00:00 2010-09-20 18:00:00 2010-09-21 17:00:00 I can correct for this by doing ->add(new DateInterval('P1DT1H'));, but adding 25 hours instead of 24 hours doesn't make any sense to me. I am using PHP Version 5.3.1. Any ideas? Thanks, W Hello, I have a query that displays a list of events for the year, and am in need of guidance on how to have it only show events that are >= today's date. The 'date' is inserted into the table as a varchar field from javascript code. There was an old blog post about doing something like.... Code: [Select] SELECT * FROM calendar WHERE `date` >= NOW() ORDER BY date ASC LIMIT 5 I seem to have trouble in that i believe NOW() will only work with proper datetime entries as opposed to my varchar setup. any help navigating this would be great. Hi, so I am trying to get how many minutes and seconds are left from the datetime entry in the database and the current datetime, but I'm having issues... Here's my code: Code: [Select] $query = "SELECT RequestMadeDateTime FROM TempAwaitingClients WHERE PSOID = '2'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $RequestMadeDateTime = $row['RequestMadeDateTime']; $currentDateTime = date('Y-m-d H:i:s'); echo 'Request Made Date Time: ' . $RequestMadeDateTime . '<br />'; echo 'Current Date Time: ' . $currentDateTime . '<br />'; echo date_diff($RequestMadeDateTime, $currentDateTime); What I'm getting those is this, "Warning: date_diff() expects parameter 1 to be DateTime" ... Which I don't get because both variables are DateTime... I am preparing $begin and $end for DatePeriod() but I can't get passed this error "getting the error Recoverable fatal error: Object of class DateTime could not be converted to string". $today = date("Y-m-d"); $datestop=$_POST['datestop']; //echo "$today<br /><br />$datestop<br /><br />"; $begin = new DateTime("$today"); $end = new DateTime("$datestop");
Hi all, I am currently storing dates and times for certain events in the following format: $now= date('Y-m-d H:i:s'); This variable is then stored in a MySQL field of type DATETIME. I am looking for a way to query the database to return all results in a certain month, or within a certain time range, or a certain year etc. How would this be achieved? e.g. If I wanted to select everything for the year 2009, would it be: SELECT * FROM table WHERE date BETWEEN '2009-01-01 00:00:00' AND '2009-12-31 11:59:59' ? Thanks! |