PHP - Date Time Parsing Question
It seems like this is a question that may be on here already, but I couldn't find it with a search, so I apologize.
I am given a string that represents a datetime, and it is always in this format: HH:mm:ss MMM DD, YYYY PDT. I just want to turn that into a proper format for mysql (Y-m-d H:i:s), but I'm not exactly sure how to go about that. (I could do this in C# rather quickly!) Probably a simple question, I hope someone can help! Thanks. Similar TutorialsHi, I am new to this forum as of now. I have a question which probably has a very straightforward answer but I just can't solve it. I am using mysql and have the following code: while($row=mysqli_fetch_array($result)) { $time1=date("j F Y G: i", $row[1]); echo "<tr><td>$row[email]</td>"; echo "<td>$time1</td>"; echo "<td>$row[comment]</td></tr>"; } It is the first line in the curly brackets that is causing the problem. If I replace $row[1] with $time I get today's date/time. But I need the date/time from what has been input so that won't work. Can anyone explain to me what is wrong with my code please? It gives the following error message: Notice: A non well formed numeric value encountered in C:\xampp\htdocs Thanks very much I'm trying to convert the following time stamp: 12:03PM to military time 13:03. I'm using the following code, but it's just outputing 12:03: $sampledate=("2010-11-08, 12:03PM CST"); $xplodeDateStamp = explode(", ", $sampledate); $xplodeDate = $xplodeDateStamp[0]; $xplodeTime = $xplodeDateStamp[1]; echo "Date: " . $xplodeDate . "<br>"; echo "Time: " . $xplodeTime . "<br>"; echo "Military Time: " . date("H:i",strtotime("$xplodeTime")); Hey guys, How would I go about subtracting Today from a previous day to find the difference? For example, I want to subtract TODAY from a previous date in my database, to determine if the difference is greater than 1 day. Any ideas? I tried doing the subraction in TIMESTAMPS, but when I convert the date back to Y-m-d H:i:s, I got some weird year and time. PHP date and time function is not showing correct time on my local system I have the following php code date_default_timezone_set("Africa/Lagos"); $date = date('d-m-y h:i:s'); echo "Server Time ".$date ."<br>"; echo "The time is " . date("h:i:sa")."<br>"; $current_datetime = date("Y-m-d") . ' ' . date("H:i:s", STRTOTIME(date('h:i:sa'))); echo "Current time1: ".$current_datetime . "<br>";
Output
Server Time 21-05-21 09:55:39
Expected Output
Server Time 21-05-21 10:55:39
Any help would be appreciated. Edited May 21 by Ponel Hello, I am doing a hotel reservation website.First am getting the checkin (arrival) and Check out(Departure) date from user.Then in next from i fetch both this values using POST method and store it in variable $arrival and $departure and then am formattind the date as i want it in the form YYYY-MM-DD(My SQL). Then am using the value of variable $arr and $dep in a query to fetch the records from DB but it is giving error.But when i do it in Hard code way I mean directly inserting the date in query it is running smmothly.Please help!!!! Am using Xampp 2.5 Heres my code... <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>ROOMS</title> <?php $arrival = $_POST['start']; $departure = $_POST['end']; $adults=6; $child=2; $room_id=101; function changeFormatDate($cdate){ list($day,$month,$year)=explode("/",$cdate); return $year."-".$month."-".$day; } // $arr="'".$arr."'"; $arr= changeFormatDate($arrival); //settype($arr, "string"); // $timestamp=strtotime($arr1); // $arr=date("Y-m-d",$timestamp); $dep= changeFormatDate($departure); // $timestamp=strtotime($dep1); // $dep=date("Y-m-d",$timestamp); ?> <style type="text/css"> <!-- .style2 { font-size: 12px; font-weight: bold; } --> </style> </head> <body> <!-- TOP --> <div id="top1"><a href="index.php"></a></div> <div id="top"> <ul class="menu"> <li class="home"><a href="index.php">Home</a></li> <li class="about"><a href="about.php">About</a></li> <li class="contacts"><a href="contact.php">Contacts</a></li> <li class="renting"><a href="gallery.php">GALLERY</a></li> <li class="selling"><a href="rates.php">RATES</a></li> </ul> </div> <!-- HEADER --> <!-- CONTENT --> <div id="content"> <div id="leftPan"> <div id="services"> <h2>RESERVATION DETAILS </h2> <p> <ul> Check In Date :<?php echo $arrival; ?><br /> Check Out Date :<?php echo $departure; ?> <br /> </ul> </p> </p> </div> </div> <div id="featured"><br /> <div> <form action="personnalinfo.php" method="post" onsubmit="return validateForm()" name="room"> <input name="start" type="hidden" value="<?php echo $arrival; ?>" /> <input name="end" type="hidden" value="<?php echo $departure; ?>" /> <input name="rooms" id="rooms" type="hidden" /> <input name="adult" type="hidden" value="<?php echo $adults; ?>" /> <input name="child" type="hidden" value="<?php echo $child; ?>" /> </div> <table bgcolor="white" border="1" width="100%" style="float:left;table-layout:fixed" cellpadding="10" cellspacing="0" > <col width="70%"> <tr> <th colspan="2" bgcolor="white"><h2><font color=maroon>Room Type</font></h2></th> </tr> <tr> <td> <table border="0" style="float:left;table-layout:fixed" width="100%"> <col width="55%"> <tr> <td valign="top"> <img src="img1/apt.jpg" style="float:left" /> </br> <div style="margin-top:120px;margin-left:5px"> <img src="img1/apt1.jpg" /> <img src="img1/apt2.jpg" /> <img src="img1/apt3.jpg" /> </div> </td> <td> <h3>Appartment</h3> <br> <span class="price">Price:</span> <span class="number">Rs. 5,000.00</span><br /> <a> Apparment in HOTEL BELLA has 2 Rooms with connecting door.It can accomodate 4 Adult and 2 children. And are located on Beach side to give you comfort and a panoramic view so that you can have a luxury accommodation.<br> *Sitting area <br>*jacuzzi shower</br> *Large terrace overlooking the sea *Jacuzzi *Light therapy *Air treatment <a href="#">more...</a></p><br /> </td> </tr> </table> </td> <td valign="top"> <table border=0 width="100%" cellspacing="10"> <tr> <td align="left"> <label><h3>People : </h3></label> </td> <td align="right"> <img src="img1/i1.jpg" /> </td> </tr> <tr> <td align="left" > <label><h3>Rooms : </h3></label> </td> <td align="right" > <?php gen_options("single")?> </td> </tr> </table> </td> </tr> <tr> <td> <table border="0" style="float:left;table-layout:fixed" width="100%"> <col width="55%"> <tr> <td valign="top"> <img src="img1/double.jpg" style="float:left" /> </br> <div id="featured"> <img src="img1/double1.jpg" /> <img src="img1/double2.jpg" /> <img src="img1/double3.jpg" /> </div> </td> <td> <h3>Double</h3> <br> <span class="price">Price:</span> <span class="number">Rs. 3,000.00</span><br /> <a>Double rooms in HOTEL BELLA has Double bed. And can accomodate 2 Adults and 2 kids. It is comfortable and pleasant, with balcony and sea view. We hope that you will enjoy your summer holidays in Bella. BASIC: Telephone. Satellite TV. Safety Deposit Box. Mini Bar - Refrigerator. Air condition. Shower with or without cabin. Hair Dryer. Balcony.<a href="#">more...</a></p><br /> </td> </tr> </table> </td> <td valign="top"> <table border=0 width="100%" cellspacing="10"> <tr> <td align="left"> <label><h3>People : </h3></label> </td> <td align="right"> <img src="img1/i2.jpg" /> </td> </tr> <tr> <td align="left"> <label><h3>Rooms : </h3></label> </td> <td align="right" > <?php gen_options("double")?> </td> </tr> </table> </td> </tr> <tr> <td> <table border="0" style="float:left;table-layout:fixed" width="100%"> <col width="55%"> <tr> <td valign="top"> <img src="img1/single.jpg" style="float:left" /> </br> <div id="featured"> <img src="img1/single1.jpg" /> <img src="img1/single2.jpg" /> <img src="img1/single3.jpg" /> </div> </td> <td> <h3>Single</h3> <br> <span class="price">Price:</span> <span class="number">Rs. 2,000.00</span><br /> <a> Single Room in HOTEL BELLA has single bed, bathroom.Can accomodated single person. is comfortable and pleasant, with balcony and sea view Telephone Satellite TV Safety Deposit Box Mini Bar - Refrigerator Air condition Hair Dryer <a href="#">more...</a></p><br /> </td> </tr> </table> </td> <td valign="top"> <table border=0 width="100%" cellspacing="10"> <tr> <td align="left"> <label><h3>People : </h3></label> </td> <td align="right"> <img src="img1/i3.jpg" /> </td> </tr> <tr> <td align="left"> <label><h3>Rooms : </h3></label> </td> <td align="right" > <?php gen_options("apartment")?> </td> </tr> </table> </td> </tr> </table> <div style="margin-top:1200px;margin-left:5px;text-align:right;"> <input type="image" src="img1/book.jpg" name="book" value="submit"/> </div> <?php function gen_options($type) { // print "$id"; // print "$type"; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("hotel", $con); $count=0; $result = mysql_query("SELECT * FROM rooms where type='$type'"); while($row = mysql_fetch_array($result)) { $a=$row['room_no']; //print "$a"; // $query1 = mysql_query("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and status='active'"); // $query=mysql_query("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and ((arrival<='2012-05-11' AND departure>='2012-05-11')OR(arrival<='2012-05-13' AND departure>='2012-05-13'))"); //$query=mysql_query("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and ((arrival BETWEEN '2012-05-11' AND '2012-05-13') or (departure BETWEEN '2012-05-11' AND '2012-05-13'))"); //$query=mysql_query("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and ((arrival<='$arr' AND departure>='$arr')OR(arrival<='$dep' AND departure>='$dep'))"); $quer=sprintf("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and ((arrival<='%s' AND departure>='%s')OR(arrival<='%s' AND departure>='%s'))",$arr,$arr,$dep,$dep); $query=mysql_query($quer); //$query=mysql_query("SELECT count(*) FROM `room_inventory` WHERE room_no='$a' and ((arrival BETWEEN '$arr' AND '2012-05-13') or (departure BETWEEN '$arr' AND '2012-05-13'))"); $r=mysql_fetch_array($query); $v=$r['count(*)']; // print "$v"; if($v==0) { $count++; // print "$count"; } } echo '<select name="room1" class="ed" id="r1">'; for($i=0;$i<=$count;$i++) { echo '<option>'.$i.'</option>'; } echo '</select>'; mysql_close($con); } // echo "$arrival\n"; // print "$departure\n"; echo "$arr"; echo "$dep"; // echo date_format($arrival, 'Y-m-d'); ?> <input type="hidden" name="result" id="result" /> </form> </div> <div class="clear"></div> </div> <!-- FOOTER --> <div id="footer"> <p><a href="index.php">HOME</a> |<a href="about.php"> ABOUT US </a>|<a href="contact.php"> CONTACTS </a>|<a href="gallery.php"> GALLERY </a>|<a href="rates.php"> ROOM RATES </a></p> </div> </body> </html> OK So I've got a datepicker that sends a date in d/m/y format. My DB stores the data in Unix Timestamp Which I can convert the date to with strtotime however this does the exact date & time. All I want is the actual day. I've spent hours trying to convert this with just the day with mixed results... Thanks. I am having a problem with PHP displaying the correct date and time. It updates as it should, but is fast by 4min and is always displaying a date in 2004. I ran a basic php script to make sure the application im using itself is not wrong. go to lunenburgledger.com/time.php Anybody had any ideas on where to check? The system time on the Windows Server 2003 is correct. The only thing I can think of is that it was converted to a virtual machine on vmware esxi, but the system time stayed right. Any ideas? Thanks! I am writing an OO PHP app to parse thousands of frames from a film, which will then each be manipulated and spat out using GD library funcs. Speed is of the essence. To improve speed, I have considered: - Another language!? - Use of PHP Command Line Interface? ( Working with web browser is surely a slow affair?) If PHP was a standard R6 Yamaha available to Joe Public, what would I have to do to win a GP race - you know - change gearing, alter timing, change exhaust system, rip out the air filter What does the panel suggest? Thanks B Hey everyone, so im trying to utilize a systems OPTION command send prior to the POST command, the OPTION command will for example state that it would like to request specific headers. im trying to perform this to get the headers needed before the POST command im trying to execute, the values are required for the POST command i need to perform to get a response. The URL in question would be: https://media-entitlement.mlb.com/api/v3/jwt?os=windows&did=ffeae86c-9447-4bac-a021-caad0c02c605&appname=mlbtv_web Request method: OPTIONS (to get required headers):
Host: media-entitlement.mlb.com then a post command is performed just after which retrieves the values originally requested by the OPTIONS command prior to the POST:
Host: media-entitlement.mlb.com
Now Generally speaking, i fully understand their authorization etc. but this specific set of values seem only used in this one situation, they use bam-sdk which i understand pretty well. but needless to say this authorization bearer, and api-key value only seem generated via the initial OPTIONS grab in browser. so in summary i need to understand how these OPTIONS commands can be utilized to store the required headers as a variable which can be used in the POST command. Thanks in advance for your time and energy responding. mosb3rgler Edited June 14, 2019 by mosb3rglertypo What is the best way to insert this time stamp into mysql? This is the info I want... Example: Sunday, 28.1.11, 9:56 How would I also select time from the database using mysql? Hey, I need help with my date & time string. I need to edit it do show in a string + also use it for calculating things like when the date has passed. The string is like this: Code: [Select] <?php $string = '2011-01-01 00:00 AM'; ?> So i wanted to check if there was a function that can automatically convert it, so it would say for example: Quote Sunday, 1st of January 2011 00:00 AM But at the same time compare $string against server time to see if the date has expired? Hope you can help Hey, I need help with date, I have stored dates in my table in this format: i.e. 22.10.2011 but now use timestamp to store dates. Problem is with this query the WHERE clause will find dates in the old format and I want to change that to the new format. Code: [Select] for($i = $count; $i > 0; $i--) { $day = date("d.m.Y", mktime(0, 0, 0, date("m"), date("d") - $i, date("Y"))); $tmp = mysql_fetch_array(safe_query("SELECT count FROM ".PREFIX."counter_stats WHERE dates LIKE '%".$day."'")); $array[] = $tmp['count'] ? $tmp['count'] : 0; } Am I making sense ? I have a field set as datetime and want to compare that field to see if it is older then ten minutes. Can anyone help I am having a brain fart? I have a batch script that reads from a timestamp.txt file to determine the last time it ran and I need to modify that by two hours. What happens is the script will run to look for changes in the county records and their server time is two hours different from mine. Code: [Select] $fr=fopen("xml/timestamp.txt",'r'); $mydate=fread($fr, 50); if ($incremental_updates == 1){ $default_template_file="xml/incremental_template.xml"; $default_config_name="xml/incremental.xml"; $contest=file_get_contents($default_template_file); $contest=str_replace("{DATE_TAG}", $mydate, $contest); $f=fopen($default_config_name,'w+'); fwrite($f, $contest); fclose($f); fclose($fr); $fw=fopen("xml/timestamp.txt",'w'); fwrite($fw, substr(date('c'), 0, 19)); fclose($fw); }else{ $default_config_name = "xml/full_update.xml"; } I tried making a time stamp update look like $cur_timestamp=date("Y-m-d H:i:s", time() +14400); which is correct but it doesn't write to the text file properly. Using the above fwrite($fw, substr(date('c'), 0, 19)); I get the correct results in the timestamp.txt file for my server time. I need it to be +14400 2011-05-25T08:27:51 would essentially become 2011-05-25T10:27:51 when it writes to the timestamp.txt I hope that makes sense. J Hi! The below dropdown will show dates/times every 15 minutes from 12am to 11:45pm for only 1 day. I want it to show for 5 days, anyway I can do this? Below code: Code: [Select] $start = strtotime('12:00am'); $end = strtotime('11:45pm'); echo '<select name="time">'; for ($i = $start; $i <= $end; $i += 900) { echo '<option>' . date('F j, Y, \a\t g:i a', $i); } echo '</select>'; Thanks for any help. Hey guys, What i would like to create is the ability to calculate the difference between two dates / times. For example: 2010-12-13 10:00:00 to 2010-12-17 17:00:00 In a format of days / hours and minutes / seconds. However, I only want to calculate for an 9-5 work day, not the hours outside. Is this possible? If so, how would I go about creating something like this. how to calculate the difference between the current datetime from the stored one(ie date_field = 2020-08-09 23:13:06) in order to compare with 15min, i mean if the resulted time exceed 15 min and not the same day(date) the provided link will show expire message. i.e $some_variable = $date_in_db - current_date; if((time() - $some_variable > 15*60) && not the same day or the same day){ echo 'the link has expired'; }
Hi, I'm preparing a code for an automated email, but the date time is not working correctly. I wanted to do a cron scheduler that emails people twice a day - 9am and 9pm. 9am sends email about records logged starting 9pm yesterday to 9am, while 9pm sends email about records logged from 9:01am to 9:00pm. The database uses date/time, so I was doing an if statement of date time, but I wasn't getting the result I wanted. Sometimes the $now time is not within the yesterday 9pm to the 9pm today range. I don't know what I'm doing wrong, but I'm sure that it has something to do with the if statement, as the some of the $now time enters the else statement Here is my code: <?php $today = date('Y-m-d 00:00:00'); $nineyesterday = date('Y-m-d H:i:s', mktime(date("H") - (date("H") + 6), date("i") - date("i"), date("s") - date("s"), date("m") , date("d"), date("Y"))); $now = date('Y-m-d H:i:s', mktime(date("H") + 13, date("i"), date("s"), date("m") , date("d"), date("Y"))); $nineam = date('Y-m-d 09:00:00'); $nine30 = date('Y-m-d 09:30:00'); $ninepm = date('Y-m-d 18:00:00'); echo '9pm yesterday: '.$nineyesterday; echo '<br/>'; echo '9am: '.$nineam; echo '<br/>'; echo '9pm: '.$ninepm; echo '<br/>'; echo 'now: '.$now; echo '<br/>'; if ((strtotime($now) > strtotime($nineyesterday)) and (strtotime($now) <= strtotime($nineam))) { //count the answers by yes or no echo 'am'; } elseif ((strtotime($now) > strtotime($nineam)) and (strtotime($now) <= strtotime($ninepm))) { //count the answers by yes or no echo 'pm'; } else { echo 'error'; } ?> Thanks in advance I have a form that has this: Code: [Select] <input type="text" name="notes_date" size="10" value="<?php echo date("m/d/Y");?>" /> I enter into the database after processing the variable like this: Code: [Select] $notes_date=date ("Y-m-d H:i:s", strtotime($_POST['notes_date'])); However, it gets stored at 2012-03-15 00:00:00 How do I fix this so that the current time gets stored instead of 00:00:00? |