Jump to content

Recommended Posts

I am trying to change the format of my db stored date.  I echoed both the $date variable and the $time variable to show you that the format I'm looking for is there, but it isn't giving the right date and time.  Here is the code:

 

while($ran = mysql_fetch_array($li)){
$lid = $ran['lid'];
$date = $ran['dtime'];
$time = date("m/d/y g:i A", $date);
}
Echo $date."<br>".$time;

 

Here's what it returns:

2008-04-29 21:30:00

12/31/69 7:33 PM

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/
Share on other sites

Use MySQL's DATE_FORMAT function.

 

The php date function expects a unix timestamp (seconds since epoc).  However, MySQL is outputting a string representation of the date.  You can either change the format in MySQL (using the DATE_FORMAT function), or you can use MySQL's UNIX_TIMESTAMP function to convert it to that format and then format the final in php.

 

I recommend the former:

 

SELECT DATE_FORMAT(dtime, "%m/%d/%y %g:%i %A") FROM table_name

 

I'm not 100% sure that the format string is correct however (I didn't look up the format).

 

http://www.mysql.com/date_format

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523451
Share on other sites

Here is what I tried based on the link you provided:

$dql = mysql_query('SELECT DATE_FORMAT(dtime, "%m/%d/%y") FROM dlg WHERE lname = "$lname"');
$dql2 = mysql_num_rows($dql);
while ($run = mysql_fetch_assoc($dql)){
$dtime = $run['dtime'];
//$time = date("m/d/y g:i A", $dtime);
}
Echo $dql2."<br>".$dtime;

 

It returned 0 (zero).  So it's not able to retrieve the data that way.  Is there something I am missing or should take away?

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523466
Share on other sites

First, by "it returned zero" do you mean it returned zero rows, or it failed?

 

Second, your query is probably wrong...you have it enclosed in single quotes, which means it will not do variable substitution...

 

change

 

$dql = mysql_query('SELECT DATE_FORMAT(dtime, "%m/%d/%y") FROM dlg WHERE lname = "$lname"');

 

to

 

$dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') FROM dlg WHERE lname = '$lname'");

 

Notice I changed double quotes to single, and single to double.

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523476
Share on other sites

OK. Here's the code I put in:

 

$dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') FROM dlg WHERE lname = '$lname'");
$dql2 = mysql_num_rows($dql);
while ($run = mysql_fetch_assoc($dql)){
$dtime = $run['dtime'];

}
Echo $dql."<br>".$dql2."<br>".$dtime;

 

Here's what it returned

Resource id #4

1

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523481
Share on other sites

Hitman have you the mysql way to do it. if you want to do it with php, the date function requires the unix timestamp to give you the correct date. Since the date in mysql is a date and not a timestamp you have to convert it.

$time = date("m/d/y g:i A", strtotime($dtime));

 

Ray

 

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523484
Share on other sites

To use what you have above.

 

$dql = mysql_query("SELECT DATE_FORMAT(dtime, '%m/%d/%y') AS dtime FROM dlg WHERE lname = '$lname'"); // add "AS dtime"
$dql2 = mysql_num_rows($dql);
while ($run = mysql_fetch_assoc($dql)){
$dtime = $run['dtime'];

}
Echo $dql."<br>".$dql2."<br>".$dtime;

 

Ray

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523485
Share on other sites

$dg1 is a resource, so it being equal to "Resource id #4" is normal.

 

$dg12 is the number of rows returned..."1" means one row was returned.

 

You didn't get a value returned back for $dtime because mysql is returning the column name back with the name "DATE_FORMAT....".  Either use a numeric index on your result...

 

while ($row = mysql_fetch_array($dg1)) {
  $dtime = $row[0];
}

 

Or, since you have one row, with one column, use mysql_result:

 

$dtime = mysql_result($result, 0);

 

Or assign the result column to a different name in the SQL query:

 

SELECT DATE_FORMAT(dtime, '%m/%d/%y') AS dtime FROM dlg WHERE lname = '$lname'

Link to comment
https://forums.phpfreaks.com/topic/102238-date-format-trouble/#findComment-523486
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.