-1

Is there a built in function in PHP that can convert date to string?

I have already tried

date("M jS, Y", strtotime("2014-04-12"));

but our client don't want this output.

Say I have a php variable holding date

$dateToday = '2014-04-12'

I want the output in this format: "Four April Two Thousand and Fourteen" or it could be "April four two thousand and fourteen"

Is there any PHP built in function through which I can do this? If not please suggest me how can I achieve this.

Blizz
  • 8,082
  • 2
  • 33
  • 53
Om Shanker
  • 49
  • 2
  • 15
  • 1
    Format your date as `4 April 2014` and then use one of the many numbers to words libraries that you can find through google – Mark Baker Apr 12 '14 at 15:21

4 Answers4

0

There is not such parameters for date() function, it's not so hard to convert this manually.

Alex
  • 8,055
  • 7
  • 39
  • 61
0

I assume you have the day, month and year in a variable.

  • Create an array with the name of the numbers 1-31,
  • Create an array with the names of the months between 1 and 12,
  • Create an array which holds the numbers between 1 and 99, for the years.

Concatenate these three things with spaces, and you have your output. There are also libraries which do this for you on the internet.

Hidde
  • 11,493
  • 8
  • 43
  • 68
0

You can simple substract the month,the year and the date separately and use a switch case for each.

This is how I did it.

$dateToday = '2014-04-12';
$year = substr($dateToday, 0, 4);
$month = substr($dateToday, 5, 2);
$date = substr($dateToday, 8, 2);
echo $dateToday.'<br/>';
switch($year) {
    case 2014 : $year_word = 'Two Thousand And Fourteen';
    //You can add as many years as you want
}
switch($month) {
    case 04 : $month_word = 'April';
    //You can add cases from 1 to 12
}
switch($date) {
    case 12 : $date_word = 'Twelve';
    //You can add cases from 0 to 31
}
$dateToday_new = $date_word.' '.$month_word.' '.$year_word;
echo $dateToday_new;

I hope this helps you. :)

prateekkathal
  • 3,482
  • 1
  • 20
  • 40
0

Use pear package Numbers_Words:

$dateToday = '2014-04-12';
$date = date('d-F-Y', strtotime($dateToday));
list($day, $month, $year) = explode('-', $date);

$NumbersWords = new Numbers_Words();
$dateWord = implode(' ', array(
    $NumbersWords->toWords($day),
    $month,
    $NumbersWords->toWords($year)
));

echo ucwords($dateWord);
steveteuber
  • 182
  • 6