Page 1 of 2

[SOLVED] Reformat A Date

Posted: Thu Jan 19, 2006 2:20 pm
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.

Posted: Thu Jan 19, 2006 4:22 pm
by duk
if the data is stored in a variable... just split the string...

Hmhm...

Posted: Thu Jan 19, 2006 5:44 pm
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.

Posted: Thu Jan 19, 2006 11:38 pm
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...

Posted: Thu Jan 19, 2006 11:40 pm
by josh
just run that sucker through strtotime

Posted: Thu Jan 19, 2006 11:40 pm
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

Hmhmhm...

Posted: Fri Jan 20, 2006 2:04 pm
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?

Posted: Fri Jan 20, 2006 2:19 pm
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

Posted: Fri Jan 20, 2006 2:21 pm
by hawleyjr
why not format the date the way you want it in your query?

Posted: Fri Jan 20, 2006 2:23 pm
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.

Posted: Fri Jan 20, 2006 2:30 pm
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.

Posted: Fri Jan 20, 2006 2:34 pm
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:

MySQL Format

Posted: Fri Jan 20, 2006 2:55 pm
by icesolid
I think I would like to see an example of how to format the date using MySQL.

Anyone?

Posted: Fri Jan 20, 2006 2:58 pm
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

Thank You

Posted: Fri Jan 20, 2006 3:10 pm
by icesolid
I preferr this method over the function. This is easy and cleaner.

Good call hawleyjr!!!