[SOLVED] Reformat A Date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

[SOLVED] Reformat A Date

Post by icesolid »

I have a date stored in a variable $date_assigned. It contains ("m-d-Y") format.

I want to print out this date but only showing it in the ("m-d") format, basically I do not want the year to show.
duk
Forum Contributor
Posts: 199
Joined: Wed May 19, 2004 8:45 am
Location: London

Post by duk »

if the data is stored in a variable... just split the string...
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Hmhm...

Post by icesolid »

There has to be a way of reformatting the date when u print it out after being stored in a variable.

If there is NOT someone please show me a code example.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

once the date has been formatted, it is no longer a date to PHP, but a string. use explode(), or use format the timestamp again...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

just run that sucker through strtotime
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

How do you get the date? If you get it via a query use a mysql function to give you the format you need.

http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Hmhmhm...

Post by icesolid »

Ok, this is how it goes.

I have a page that prints out a table of data from a database. It prints the following fields.

$policy_number
$first_name
$date_assigned


$date_assigned being a date that was stored into the database when the case for the user was assigned. When the date was stored it was stored like this date("m-d-Y");

I want to print it out of the database now but I do not want to have the year print out, just the month and day.

I figured this was an easy one, I dunno.

Help, Please?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

I insert and format dates using

Code: Select all

function insertDate()
{
	$timestamp 	= time()+date("Z");
	$time		= gmdate("Y-m-d H:i:s",$timestamp);
	return $time;
}
which shows like: 2006-01-19 11:39:20


And then retrieve and format using

Code: Select all

function niceDate($daterow)
{ 
      	$day 			= substr($daterow, 8, 2);
      	$month 			= substr($daterow, 5, 2);
      	$year 			= substr($daterow, 0, 4); 
      	$date 			= "$month-$day-$year";
      	return $date;
}
which shows like: 01-19-2006

So using my example, to not have the year show you would
just take out the -$year from the $date
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

why not format the date the way you want it in your query?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

hawleyjr wrote:why not format the date the way you want it in your query?
Because I dont allow flooding. So I still need the time with the date.
And I have other uses for full date/time....

I would rather have all info in one row and use whats needed when needed
compared to many rows with seperate info.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I believe he/she is regarding a date field not date time.
When the date was stored it was stored like this date("m-d-Y");
I would rather have all info in one row and use whats needed when needed
compared to many rows with seperate info.
I disagree; why not let mysql do the work? By doing it via a PHP function you are just adding extra unnecessary work.

IMHO ~ MySQL functions are extremely underrated.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

hawleyjr wrote:I believe he/she is regarding a date field not date time.
When the date was stored it was stored like this date("m-d-Y");
They were asking how to format a date.
My post showed how to format a date regardless of how it was stored.
hawleyjr wrote:
I would rather have all info in one row and use whats needed when needed
compared to many rows with seperate info.
I disagree; why not let mysql do the work? By doing it via a PHP function you are just adding extra unnecessary work.

INHO ~ MySQL functions are extremely underrated.
p.o.
personal opinion :roll:
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

MySQL Format

Post by icesolid »

I think I would like to see an example of how to format the date using MySQL.

Anyone?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Gladly :)

http://dev.mysql.com/doc/refman/5.0/en/ ... #id3127979

Code: Select all

SELECT id,DATE_FORMAT(date_field,'%m %d') as some_date_name from my_table
This query will give you a record id and a date formatted as month [space] Day of the month
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Thank You

Post by icesolid »

I preferr this method over the function. This is easy and cleaner.

Good call hawleyjr!!!
Post Reply