Date Convert

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

Post Reply
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Date Convert

Post by anyday »

anyone know how to convert a unix style 07,08 date to english like jan, feb, mar?
AngryPanda
Forum Newbie
Posts: 16
Joined: Wed Jul 19, 2006 12:18 am

Post by AngryPanda »

anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Post by anyday »

i looked at those 3 functions and none can do what i need them to.

i have a var named $this_month which contains a digit 1-12. i need to have this 1-12 converted into the months spelled out like janurary - december?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You could store an array of months, however a combination of two of the funcitons mentioned by AngryPanda will do it as well.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I usually set up a week day array and a month array, like this...

Code: Select all

<?php
/**********************************************************
 * Days of the Week array
 *********************************************************/
$days_array = array(
	"Sunday", 
	"Monday", 
	"Tuesday", 
	"Wednesday", 
	"Thursday", 
	"Friday", 
	"Saturday"
);

/**********************************************************
 * Develop an array of months
 *********************************************************/
$months_array = array(
	1 => 'January',
	2 => 'February',
	3 => 'March',
	4 => 'April',
	5 => 'May',
	6 => 'June',
	7 => 'July',
	8 => 'August',
	9 => 'September',
	10 => 'October',
	11 => 'November',
	12 => 'December');
?>
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

What's wrong with this?

Post by ronverdonk »

You said to have a Unix timestamp (i.e. the result of a PHP time()). Why does the following not give you what you want?

Code: Select all

<?php
echo date("F", time());
?>
It converts a unix timestamp to the full month and that is what you wanted, hmm?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Lets say all you know about the selected month is that it is month number 8. What PHP function tells you the name of the month? date() does not do that.
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Post by anyday »

everah,

thanks for the help. Im a big newbie here . got any tips on how i would link up my $this_month var with the array?
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Still don''t need an array

Post by ronverdonk »

Everah wrote:Lets say all you know about the selected month is that it is month number 8. What PHP function tells you the name of the month? date() does not do that.
You can if you just insert the month into a date field, like this:

Code: Select all

$m="08";
$d="2000-$m-04";
echo date("F", strtotime($d));
Still don't need an array!
anyday
Forum Newbie
Posts: 8
Joined: Fri Jul 21, 2006 6:06 pm

Post by anyday »

thanks that worked great
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

Bit of a hack though if you just copied and pasted his code... use this rather.

Code: Select all

function getMonthAsString($month)
{
    $unix_timestamp = mktime(null, null, null, trim($month, '0') + 1, null, null);
    return date("F", $unix_timestamp);
}
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

My only point was that you don't need an array to get to a month description.
Post Reply