CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Problems with my date format (http://www.codingforums.com/showthread.php?t=283970)

jeddi 12-11-2012 01:08 PM

Problems with my date format
 
Hi,

Today is 11th December 2012

I have my date stored in my MySql dbase as a datetime
in this format: 2012-12-11 00:00:00

I want to output it as m/d/y format.

So I would like: 12/11/12


I have tried this:

PHP Code:

$N_pub_date     "{$row['pub_date']}";
$N_pub_date date("m/d/y"$N_pub_date); 

echo 
$N_pub_date 

But I am getting this: 12/31/69

Anyone know how I correct this problem ?

Thanks


.

idalatob 12-11-2012 01:15 PM

The problem is, the date function expects a timestamp as the second parameter and not a mysql date.

Try this:

PHP Code:

$N_pub_date     strtotime("{$row['pub_date']}"); 
$N_pub_date date("m/d/y"$N_pub_date);  

echo 
$N_pub_date 


abduraooft 12-11-2012 01:32 PM

Quote:

I have my date stored in my MySql dbase as a datetime
in this format: 2012-12-11 00:00:00

I want to output it as m/d/y format.

So I would like: 12/11/12
It'd better to use the function date_format() in mysql itself.

jeddi 12-11-2012 02:20 PM

Hi,

Thanks for replies.

I tried:

PHP Code:

$N_pub_date     strtotime("{$row['pub_date']}"); 
$N_pub_date     date("m/d/y"$N_pub_date); 

But I still get: 12/31/69

I looked at the MYSQL link and think I need this:

PHP Code:

SELECT DATE_FORMAT('2009-10-04 22:23:00''%m/%d/%Y'); 

But how do write that into my php code ?

Thanks

Fou-Lu 12-11-2012 02:46 PM

I would suspect you have a cached page there. If pub_date is 2009-10-04 22:23:00, that is valid.
The DATE_FORMAT is used in SQL, not in PHP. Simply modify the query and add the DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate, then pull the formattedDate from the resultset.

jeddi 12-11-2012 06:58 PM

So my query is :
PHP Code:

$sql "SELECT * FROM pages WHERE page_id = $Db_page_id AND client_no = '$this_client' "

So if I want to include

DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate

then I'll have to list all the records I guess ??
( there are about 18 of them )

It seems to me it would be nicer to just reformat the result for that record.

Something like :

PHP Code:

$N_pub_date     strtotime("{$row['pub_date']}"); 
$N_pub_date     date("m/d/y"$N_pub_date); 

Can it not be done with php ??

Thanks.


.

Fou-Lu 12-11-2012 07:29 PM

This is fine:
PHP Code:

$N_pub_date     strtotime("{$row['pub_date']}"); 
$N_pub_date     date("m/d/y"$N_pub_date); 

So long as $row['pub_date'] represents a valid datetime string in SQL and strtotime is available (which it has been since like, 4.2 or something like that). So if I do this:
PHP Code:

$row = array('pub_date' => '2009-10-04 22:23:00');
$N_pub_date     strtotime("{$row['pub_date']}"); 
$N_pub_date     date("m/d/y"$N_pub_date);
printf('Date: %s'$N_pub_date); 

I get this:
Code:

Date: 10/04/09
As for SQL, although I'd suggest querying for each property separately, you can mix and match wildcards with explicits. SELECT *, DATE_FORMAT(pub_date, '%m/%d/%Y') AS formattedDate FROM. . . is fine.

jeddi 12-11-2012 08:29 PM

Thanks :thumbsup:

I have it working now :)


All times are GMT +1. The time now is 05:26 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.