Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-11-2012, 01:08 PM   PM User | #1
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,512
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
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


.
__________________
If you want to attract and keep more clients, then offer great customer support.

Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
jeddi is offline   Reply With Quote
Old 12-11-2012, 01:15 PM   PM User | #2
idalatob
Regular Coder

 
Join Date: Sep 2007
Location: Grahamstown, South Africa
Posts: 237
Thanks: 6
Thanked 17 Times in 17 Posts
idalatob is on a distinguished road
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 
idalatob is offline   Reply With Quote
Old 12-11-2012, 01:32 PM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
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.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-11-2012, 02:20 PM   PM User | #4
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,512
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
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
__________________
If you want to attract and keep more clients, then offer great customer support.

Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
jeddi is offline   Reply With Quote
Old 12-11-2012, 02:46 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-11-2012, 06:58 PM   PM User | #6
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,512
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
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.


.
__________________
If you want to attract and keep more clients, then offer great customer support.

Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
jeddi is offline   Reply With Quote
Old 12-11-2012, 07:29 PM   PM User | #7
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 12-11-2012, 08:29 PM   PM User | #8
jeddi
Senior Coder

 
Join Date: May 2006
Posts: 1,512
Thanks: 26
Thanked 4 Times in 4 Posts
jeddi has a little shameless behaviour in the past
Thanks

I have it working now
__________________
If you want to attract and keep more clients, then offer great customer support.

Support-Focus.com. automates the process and gives you a trust seal to place on your website.
I recommend that you at least take the 30 day free trial.
jeddi is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:07 AM.


Advertisement
Log in to turn off these ads.