Jump to content

DATE FORMAT


johnseito

Recommended Posts

ok thanks. so How can I get the date out and manipulate it? 

I didn't really set a variable for it in PHP, and I use this code and I can export everything out.

 

$sql = "SELECT * FROM tlb";
$result = mysql_query($sql, $conn);
   
      while ($row = mysql_fetch_array($result)) {
    
      }
    

 

This code even pulls the dates out, because it's pulling everything from the tlb array.

 

so how do I pull the date out and manipulate it, and if I didn't want to pull it out how do I set PHP to do that?

 

 

Link to comment
Share on other sites

function dateformat($from="",$format="M d, Y")
{
   if(empty($from))
          $ft=time();
   else
          $ft=strtotime($from);
  return date($format,$ft);
}

$sql = "SELECT * FROM tlb";
$result = mysql_query($sql, $conn);
   
      while ($row = mysql_fetch_array($result)) {
         $row['dt']=dateformat($row['dt']);
      }
[code]

[/code]

Link to comment
Share on other sites

Don't use "SELECT * FROM ... ", specify the columns you need (unless you really do need all of them). e.g.

 

SELECT id, name, entrydate FROM ....

 

I changed the code to this:

 

$conn  = mysql_connect("localhost", "username", "psswrd");
mysql_select_db("db", $conn);
$sql = "SELECT name FROM info";
$result = mysql_query($sql, $conn);
   
      while ($row = mysql_fetch_array($result)) {

      }

 

This code gave me everything, not just the name as I wanted.

why is that? you know what I can do to change it?

 

Link to comment
Share on other sites

Using any computer based language, like the SQL query language, requires that you know the basics to be effective at using it.

 

On the third page at the link that Barand posted is how you select specific information from a database. If you did not get that far or did not understand what was on that page, then go back and reread all the pages that are part of that tutorial.

 

What you are asking, to only return the name you wanted, is a fundamental concept of using a database. If you are asking how to do that, then you need to spend more time getting up to speed on the overall concept of using databases before you attempt to actually write or use code. This is a little like getting into a car to drive it and knowing that there are gas and brake pedals and what each of them does before you start the engine.

Link to comment
Share on other sites

PFMaBiSmAd -

you're right, thanks.  I do have sql experience I work with it, I export datas out of it from database.  just a bit new to the intergation of PHP and Mysql

 

 

function dateformat($from="",$format="M d, Y")
{
   if(empty($from))
          $ft=time();
   else
          $ft=strtotime($from);
  return date($format,$ft);
}

 

what is the $from"",  I put $from="entrydate"

 

         $row['dt']=dateformat($row['dt']);

 

what is 'dt"?  I put dt as entrydate not sure if that would be correct

   

Link to comment
Share on other sites

 

DATE_FORMAT( entry_date, <your desired format> )?

 

I tried this but it gaves me an error,

 

then I tried this and it gave me a date but not the date that was in the database, seems to be some date that was already set.

 

$datetime = strtotime('$entrydate');

echo date("l, jS F, Y h:i:s a", $datetime);

Link to comment
Share on other sites

this is why i gave u a function to use

function dateformat($from="",$format="M d, Y")
{
   if(empty($from))
          $ft=time();
   else
          $ft=strtotime($from);
  return date($format,$ft);
}

 

it's a function, u call it from yer code, not change it (unless u know what u are doing).

in my original post i gave an example of how to use it

$sql = "SELECT * FROM tlb";
$result = mysql_query($sql, $conn);
   
      while ($row = mysql_fetch_array($result)) {
         $row['dt']=dateformat($row['dt']);
      }

 

for a more precise description of what it does: dateformat converts a date string to another date format.

the calling is simple

$var=dateformat($olddate,$newformat)

$var is the variable to hold the new date

$olddate, is the variable holding the old date

$newformat is how u want the new date to look like, use date function to help u try different formats. this parameter is optional as a default format is set in the function.

Link to comment
Share on other sites

 

DATE_FORMAT( entry_date, <your desired format> )?

 

I tried this but it gaves me an error,

 

then I tried this and it gave me a date but not the date that was in the database, seems to be some date that was already set.

 

$datetime = strtotime('$entrydate');

echo date("l, jS F, Y h:i:s a", $datetime);

This is a MYSQL function, not a php function.

Link to comment
Share on other sites

Look I got a Date_format code from the website:

 

   $query ="SELECT entrytitle, entrytext,";
   $query.=" DATE_FORMAT(entrydate, '%M %d, %Y') AS date";
   $query.=" FROM tbl ORDER BY entrydate DESC LIMIT 10";
   $result=mysql_query($query);
   while (list($entrytitle,$entrytext,$entrydate) = 
    mysql_fetch_row($result)) {
       echo "<dt><b>$entrytitle ($entrydate)</b></dt>";
       echo "<dd>$entrytext</dd>";

 

and it looks like it is a php function, i mimic this and it didn't work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.