Set date ordinal for last day of the month

I am using

echo date(‘t’);

To display the no of days in the current month

I would also like to display the ordinal that goes with it

ie if there are 30 days in the month it would show ‘30th’

If I use

echo date(‘tS’); I just get the ordinal for today

Is there a similar shortcut or am I going to have to do some sort of lookup/calculation?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

You need to tell it what day to use. Try this:

$days = date('t', time());
$month = date('m',time());
$year = date('Y',time());
$last_date = strtotime("$year-$month-$days 00:00:01");
$last_date_ordinal = date('tS',$last_date);
print 'The ' . $last_date_ordinal . ' is the last day of this month';

Quite a lot of hassle for four characters, but there you go.

If you know what the first of next month is, you can count back one
day to get the last day of this month as well. For example, this will
return 28th (of this February):

date('tS',strtotime('2011-03-01 - 1 day'));

So we could try this:

$next = explode('-',date('Y-n-01'));
$next[1] = ($next[1] + 1 > 12) ? 1 : $next[1] + 1;
date('tS',strtotime(implode('-',$next) . ' - 1 day'));

Let me know how that works.

Walter

On Feb 23, 2011, at 5:37 PM, DeltaDave wrote:

I am using

echo date(‘t’);

To display the no of days in the current month

I would also like to display the ordinal that goes with it

ie if there are 30 days in the month it would show ‘30th’

If I use

echo date(‘tS’); I just get the ordinal for today

Is there a similar shortcut or am I going to have to do some sort of
lookup/calculation?

David


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Walter - I will try that

D


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Ah, Walter beat me to it! :slight_smile: i was scratching my head over this one for a while but here’s what I came up with;


<?php
$last_day_of_month = strtotime(date("Y-m-t"));
echo date('tS', $last_day_of_month);
?>

Basically you need to create a date object for the last day of the month and then get the ordinal for that.
Regards,
Tim.

On 23 Feb 2011, at 23:33, Walter Lee Davis wrote:

You need to tell it what day to use. Try this:

$days = date(‘t’, time());
$month = date(‘m’,time());
$year = date(‘Y’,time());
$last_date = strtotime(“$year-$month-$days 00:00:01”);
$last_date_ordinal = date(‘tS’,$last_date);
print ‘The ’ . $last_date_ordinal . ’ is the last day of this month’;

Quite a lot of hassle for four characters, but there you go.

If you know what the first of next month is, you can count back one day to get the last day of this month as well. For example, this will return 28th (of this February):

date(‘tS’,strtotime(‘2011-03-01 - 1 day’));

So we could try this:

$next = explode(‘-’,date(‘Y-n-01’));
$next[1] = ($next[1] + 1 > 12) ? 1 : $next[1] + 1;
date(‘tS’,strtotime(implode(‘-’,$next) . ’ - 1 day’));

Let me know how that works.

FreewayActions.com - Freeware and commercial actions for Freeway Express & Pro.

Protect your mailto links from being harvested by spambots with Anti Spam.
Only available at FreewayActions.com

http://www.freewayactions.com


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Thanks Tim I will try that too.

D


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Multiple skinning of Cat - thanks guys. Just shows there are many routes to the same destination.

Walter just a question about your 2nd helping

$next = explode('-',date('Y-n-01'));......

Is this a one time thing ie it only displays the the 28th (relative to the

date('tS',strtotime('2011-03-01 - 1 day'));

So when March comes round it still thinks that March is the next month.

D


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

No, my next formats the first of this month, Whatever month it currently is. Then the explode breaks it into an array, and the following line modifies the second element of that array, incrementing it or setting it to 1 if it goes over 12. But Tim’s is much much better than mine. That’s what I wish I had thought of.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Well all options work and I am grateful to get even 1 solution.

I can’t wait till the end of the month to see it work properly.

Thanks

D


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options