I want to display the current date in this format but displaying the words dosn’t seem an option under the date variable formatting.
twenty october twenty ten
Is there a way to do this in EE?
This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
October 19, 2010 4:24pm
Subscribe [2]#1 / Oct 19, 2010 4:24pm
I want to display the current date in this format but displaying the words dosn’t seem an option under the date variable formatting.
twenty october twenty ten
Is there a way to do this in EE?
#2 / Oct 19, 2010 6:24pm
Plugin would be easiest. There’s already one for relative dates:
two days ago.But I’ve not seen one that does what you want. It shouldn’t be too bad though to do something simple that’ll spit out something in the format you do above.
Jamie
#3 / Oct 20, 2010 1:52pm
I’ve hacked this script to get it working for now, seems a bit bloated for what I need but it works. Not sure how to turn it into an EE add-on though. I hacked my bit on the end to get it to do what I wanted, although I’m sure my bit is coded real badly, but I’m relatively new to PHP. I had to get it to just print ‘Twenty’ as I’m not sure how to split the year ‘2010’ into 2 parts so it doesn’t just read ‘two thousand ten’.
$day = date("j");
$month = date("F");
$year = date("y");
try
{
echo convert_number($day);
echo ($month);
echo "Twenty";
echo convert_number($year);
}<?php
/**
* Function: convert_number
*
* Description:
* Converts a given integer (in range [0..1T-1], inclusive) into
* alphabetical format ("one", "two", etc.)
*
* @int
*
* @return string
*
*/
function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
throw new Exception("Number is out of range");
}
$Gn = floor($number / 1000000); /* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000); /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100); /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10); /* Tens (deca) */
$n = $number % 10; /* Ones */
$res = "";
if ($Gn)
{
$res .= convert_number($Gn) . " Million";
}
if ($kn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($kn) . " Thousand";
}
if ($Hn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($Hn) . " Hundred";
}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen");
$tens = array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety");
if ($Dn || $n)
{
if (!empty($res))
{
$res .= " and ";
}
if ($Dn < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];
if ($n)
{
$res .= "-" . $ones[$n];
}
}
}
if (empty($res))
{
$res = "zero";
}
return $res;
}
$day = date("j");
$month = date("F");
$year = date("y");
try
{
echo convert_number($day);
echo ($month);
echo "Twenty";
echo convert_number($year);
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>#4 / Oct 20, 2010 1:57pm
It’s not too hard especially if you just want it to handle numbers up to 99 which is all you need to do the example you showed:
<?php
function number_to_words($number)
{
$words_to_19 = array(
"",
"One",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Eleven",
"Twelve",
"Thirteen",
"Fourteen",
"Fifteen",
"Sixteen",
"Seventeen",
"Eightteen",
"Nineteen"
);
$words_to_90 = array(
"",
"",
"Twenty",
"Thirty",
"Fourty",
"Fifty",
"Sixty",
"Seventy",
"Eigthy",
"Ninety"
);
if ($number < 20)
{
return $words_to_19[$number];
}
else
{
$n_chunk_1 = substr($number, 0, 1);
$n_chunk_2 = substr($number, 1, 1);
return $words_to_90[$n_chunk_1].'-'.$words_to_19[$n_chunk_2];
}
}
$date = 'August 11, 1978 2:00AM';
$time = strtotime($date);
$year = date('Y', $time);
$y_chunk_1 = substr($year, 0, 2);
$y_chunk_2 = substr($year, 2, 2);
$year = number_to_words($y_chunk_1).' '.number_to_words($y_chunk_2);
$date = number_to_words(date('j', $time)).' '.date('F', $time).' '.$year;
print_r($date); exit();
?>Converting that to a simple plugin would be pretty easy. Since I’m using string_to_time you could just pass the date to the plugin as a parameter easily enough.
Jamie
#5 / Oct 20, 2010 1:59pm
Oh, you’re version is a bit better in that handles larger numbers. Anyway. There you go either way.
#6 / Oct 20, 2010 3:54pm
That looks a lot cleaner! I might try and put that into a plug-in for practise.
Thanks