1

I have a string like 09-10 which is representative of mm-dd. I need it in a format something like Monday 10th September? The problem is that I do not have a year and I can't have an array containing months and days because I would like to know the day of the week (Mon, Tue, Wed etc.)

Any idea how to do this in PHP, preferably using date() to format the date? Note: this is not in MySQL...

1
  • If the year it's not important, put any year on your string and when you print the date, print only what you need, ignoring the fake year... but, by default, you can't get what you need without knowing the year. :( Commented Apr 10, 2012 at 10:58

2 Answers 2

8

You can't get the day (Monday, Tuesday etc) without knowing the year.

You can use date('jS F', strtotime('2012-09-10')); to get the day of the month and month, just shove any old year in there. I'd make sure to use a leap year year though to make sure you catch those pesky feb dates properly.

Example: http://codepad.org/JfUeTQlH

So, like this:

$d_m = '09-10';

$my_date = date('jS F', strtotime('2012-'.$d_m));
3
  • Ah. I should have thought about this more. Could I just get something like 10th September then, without using an array containing the months?
    – q3d
    Commented Apr 10, 2012 at 10:57
  • If you want to get 10th September as an output from 09-10 then you can append any year e.g. current year and format that string to get your desired output. Commented Apr 10, 2012 at 11:01
  • 1
    Use mktime(12,0,0,$month,$day) and use this timestamp to create your desired output format using date or strftime
    – Basti
    Commented Apr 10, 2012 at 11:03
0

As it is clear Every 1st day of year is not Sunday, it happens after regular interval So just saying a date without year is not clear about day(Monday,Tuesday...). it will give you number of day in that year i.e. out of 365days. so if you are working within a year its OK, but if it goes beyond it You wont be able to get the day(Monday,Tuesday...).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.