PHP - Syntax Question, Formatting Date From A Sql Recordset...
Hi all, new to PHP and had a syntax question. I have the following code:
print "<p>{$row['comment_createdate']}; This prints out a date from my sqlquery as a database datetime. I need to format it to display "Monday June 15, 2010 5:15 pm". I'm not sure how/where to attach a date() function to the record. (If it matters, the line above is inside of a loop going through several records). This really applies to attaching functions to columns within a recordset in general. Many thanks ahead of time. Similar TutorialsHi guys, I'm putting together a small event system where I want the user to add his own date and time into a textfield (I'll probably make this a series of drop-downs/a date picker later). This is then stored as a timestamp - "0000-00-00 00:00:00" which displays fine until I try to echo it out as a UK date in this format - jS F Y, which just gives today's date but not the inputted date. Here's the code I have right now: Code: [Select] $result = mysql_query("SELECT * FROM stuff.events ORDER BY eventdate ASC"); echo "<br />"; echo mysql_result($result, $i, 'eventvenue'); echo ", "; $dt = new DateTime($eventdate); echo $dt->format("jS F Y"); In my mysql table eventdate is set up as follows: field - eventdate type - timestamp length/values - blank default - current_timestamp collation - blank attributes - on update CURRENT_TIMESTAMP null - blank auto_increment - blank Any help as to why this could be happening would be much appreciated, thanks. Hi, I have an affiliate system that shows results on what product is purchased. The problem is that the tracker is only listing the first product in the purchase list, so if someone purchases multiple products it only shows the first product in the transaction report. Here is the code: Code: [Select] $module = fetchDbConfig('PostAffiliatePro'); $orderInv = $order->getOrderInv($_GET["cart_order_id"]); print_r($orderInv); $affCode = "<!-- Begin PostAffiliatePro Affiliate Tracker --> <img border='0' src='".$module['URL']."/scripts/sale.php?TotalCost=".sprintf("%.2f", $orderSum['subtotal'])."&OrderID=".$cart_order_id."&ProductID=".$orderInv['0']['productCode']."' width='0' height='0' alt='' /> <!-- End PostAffiliatePro Affiliate Tracker -->"; ?> The code I need help with syntax is: Code: [Select] "&ProductID=".$orderInv['0']['productCode']."The products are split into arrays by number (0,1,2,3). So for example, I would want to show results for .$orderInv['0']['productCode']. , then .$orderInv['1']['productCode']. , and so on. I would like the results to be posted with a ; between them, but I don't know the proper syntax to have it report more than just the first array. This seems pretty simple, but I only know the basics of PHP and I've adapted this from found code. Any guidance would be greatly appreciated. Thanks! Hi Simple question, how do I reFormat a date. $dateCreated (2012-01-14) to become: 14th jan 2012 So far I have this, but it's not working: Code: [Select] $expDate = explode('-',$dateCreated); $date1 = mktime(0, 0, 0, date('m', $expDate[1]), date('d', $expDate[2]), date('Y', $expDate[0])); $date = date('d m Y', $date1); Thanks Hi, When I do this the result is a date of zeros? $DateAndTime = date('d-m-y', strtotime($DateAndTime)); All I want is a date in format of DD0MM-YYY and the Time in HH:MM::SS Any help would be great! I know it is basic, Thanks in advance I'm going round in circles with this one, but I have a feeling that the answer is obvious. I have POST information that I want to use as a date and display it using date format, but I don't know how to The POST data outputs the following: 2011-9-23 Assuming I have defined $date as the posted data from the previous form, how do I write some code to tell it to output it as September 23, 2011, or just Sept 2011? I'm not sure what I'm doing wrong. I have a date in a table which is formatted as: 2010-08-13 00:00:00. I am trying to change the date through PHP to display as 08-13-2010. The date is being formatted as 12-31-1969. Here's my code. if($registrant->StatusDate != '0000-00-00 00:00:00') { $formattedDate = date('m-d-Y', $registrant->StatusDate); $statusDate = ' as of ' . $formattedDate; } Can anyone advise me what I am doing wrong? Thanks! Having used unix timestamps in the past formatting the date used to be easy. But since converting to mysql DATETIME im struggling to format it properly. I basically pull the date from the database, convert it using strtotime(), then convert it to a date using date(). The problem is that when i use 'a' to display am/pm the time almost always says am. If the time is 11am then it will display 11am. If it's 12pm then it will display 12pm, but if it comes to 1pm and above it will revert back to 1am. This is the date string I use: date('jS M Y g:i a'); is this due to the fact that mysql DATETIME uses 24 hour clock ('H'), if so is there a way around this? Hi all I have a PHP script with the following var print $date; at the moment it is formatting the date like 03-24-2010 is it possible to get PHP to switch the date and month, so it looks like 24-03-2010 Thanks Hi all.. I'm grabbing the following date format from an rss feed: Sun, 16 Jan 2011 00:00:00 -0800 What's the best way to strip away the first 5 characters (Sun, ) and the last 15 characters ( 00:00:00 -080)... but still keeping this as a date (not a string) to store in my database? Thanks in advance for any ideas! Hi Guys, I have got a date string which looks like the following: Quote Thu Feb 09 2012 07:25:00 GMT 0000 (GMT Standard Time) I need to convert it to Quote d-m-Y H:i format. Any ideas on how to do this? Thanks! I have selected datetime values from a MySQL database (originally stored with the NOW() method) and want to present them on a page formatted in a specific way. I succeded in making it work with the following php code:
$time = date('d/m Y H:i', strtotime($row['time'])); echo $time;I am however unsure if this is the best way to do it? I noticed some comments at php.net stating that this method has some limitations due to UNIX time. I am confused. There seem to be many ways of formatting date and time. Maybe somebody can clarify the situation, and tell me what will be appropriate in the current situation? I will appreciate any suggestions ... Erik Edited by erikla, 17 July 2014 - 10:01 AM. To echo this: Posted on: October 23rd 2011 ... I'm querying my db with this: DATE_FORMAT(creation_date, '%M %D %Y') What if I wanted to echo this: 23. Oktober, 2011 ... that is, to display the date as it would be shown in German? TIA if you can help. I must admit that dates and timestamps really confuse me! I have a query that gets a field named "date_purchased" and returns it like this "2010-09-05 09:58:12" What I need to do is add one year to the date, and display it like this "September 5, 2011" Basically, it's displaying the end date of a one year subscription. The database captures the purchase date with the order, so I want to grab that, add a year, and display it to the customer. Any help would be most appreciated! How would I put today date for instance into mysql_query? I just want the date and have the format be year, month, day. The format in the database looks like 2010-04-05. That being used as an example. And I have three drop down menus, which you can select the year, month, and day, but it doesn't seem to be working. What am I doing wrong? I have the date stored as: date( "d/m/Y" ) After it's pulled from the database, I would like it to display like: date( "F j, Y" ) I have tried below, but the output makes d the month and m the day. $dateStamp = strtotime($row['startrepeat']); $dateFormatted = date("F j, Y", $dateStamp); Currently the date is stored in the database as 28-02-2011 - 21:00:30 and the variable is: $date['mTime'] How can I format it too look "pretty" like this: February 28, 2011 - 10:00 PM? (don't need the seconds) Ok, so I have some code that takes the records in my database and outputs them in a table, but currently after the first record it starts spacing them incorrectly
Here is the code
<?php require_once('config.php'); require_once('menu.php'); echo '<h1>View All Alien Interactions</h1>'; echo '<table> <tr>'; foreach($fields AS $label){ echo "<th>{$label}</th>"; } echo '<th>Edit</th><th>Delete</th>'; echo '</tr>'; $fields_str = '`contact_id`, `'.implode(array_keys($fields), '`, `').'`'; $sql = "SELECT {$fields_str} FROM `alien_abduction`"; foreach($dbh->query($sql) as $row) { echo '<tr>'; foreach($fields AS $field=>$value){ echo '<td>'.(isset($row[$field]) && strlen($row[$field]) ? $row[$field] : ' '.'</td>'); } echo '</tr>'; echo '<td><a href="edit.php?contact_id='.$row['contact_id'].'">Edit</a></td>'; echo '<td><a href="delete.php?contact_id='.$row['contact_id'].'">Delete</a></td>'; echo '</tr>'; echo '</table>'; } ?>I just want it to start a new line after importing each record This is a picture of what its curently doing, look at the second row, it just keeps adding all additional entries on this line (I whited out personal info) Edited by tekkenfan2, 30 June 2014 - 01:06 PM. Hi,
I'm very new to PHP, 2 days in fact, but I've done some programming in other languages. I'm trying to do something pretty basic here. I've inherited a site built on WordPress and I'm trying to override one of the php files contained in a wordpress plugin to do what I want.
I have this line:
<label>
<?php if ( ! $account_required ) echo '<small>' . __( '(optional)', 'wp-jm' ) . '</small>'; ?>
</label>
I understand what the code is doing, if condition is false, then output the HTML <small> tag, I want to add and else condition to the above, that outputs the word "required" in place of where the word optional is. So if $account_required = false then shows the optional in parenthesis as it does now, otherwise I want to show the word "required" in the parenthesis. Super simple, but just can't get the syntax quite right.
Also, can someone explain the double underscore part, specifically this: __( '(optional)', 'wp-jm' ) I understand in PHP periods are used to concatenate, but what is the leading double underscore right before the word "optional" in parenthesis? it's as if there is some function being called or referenced. The end result when the HTML is rendered is "(optional)", but what's up with the leading underscore and the reference to "wp-jm" I assume that there is some plugin or something...
Any help appreciated.
Thanks....
Edited by westside, 11 September 2014 - 05:30 PM. Code: [Select] <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post"> <input type="submit" name="submit" value="Refresh" /> </form> the above works fine for a refresh button ... but does not if I replace <? with <php? I often see the colon ( : ) being used in syntax however, no amount of searching through my text books or checking php.net reveals to me exactly what this is used for, when it is used and the exact definition of it's value. Intuitively it seems to mean ' or ' but I can't be sure when || is the proper operator for 'or' Can anyone point me towards a url for a succinct explanation ? thanks VJ |