Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

date_create

0 views
Skip to first unread message

Jeff

unread,
Oct 10, 2008, 2:05:40 AM10/10/08
to
I'm porting some perl (a calendar) over to php and I have some date
questions.

What I'd like to do is feed in a month and year and get the number of
days in the month and the starting day of the week.

The date function does this:

$day_of_week = date('w',$unix_timestamp);
$days_in_month = date('t',$unix_timestamp);

Now, I don't know how to get the timestamp easily.

I thought I could do this:

$date_obj = date_create();
date_date_set($date_obj,$year,$month,1);

But I don't know how to get the timestamp out of the date_obj. What's
the function for that?

Is there an easier way to do this?

Perl is a bit erratic in how it indexes months and years. I didn't see
much in the docs but:

Is the month "1" indexed (ie, January is 1)?
Is the day also 1 index?
Is the year a 4 digit year?

That would be sensible which is not the case in Perl!

Jeff

Geoff Berrow

unread,
Oct 10, 2008, 3:05:10 AM10/10/08
to
Message-ID: <Q8Wdnd8W-L4lbXPV...@earthlink.com> from Jeff
contained the following:

>The date function does this:
>
> $day_of_week = date('w',$unix_timestamp);
> $days_in_month = date('t',$unix_timestamp);
>
> Now, I don't know how to get the timestamp easily.

http://uk.php.net/date
--
Geoff Berrow 0110001001101100010000000110
001101101011011001000110111101100111001011
100110001101101111001011100111010101101011
http://slipperyhill.co.uk - http://4theweb.co.uk

"Álvaro G. Vicario"

unread,
Oct 10, 2008, 3:30:38 AM10/10/08
to
Jeff escribió:

> What I'd like to do is feed in a month and year and get the number of
> days in the month and the starting day of the week.
>
> The date function does this:
>
> $day_of_week = date('w',$unix_timestamp);
> $days_in_month = date('t',$unix_timestamp);
>
> Now, I don't know how to get the timestamp easily.

You can use mktime(). It has the interesting feature of "fixing" wrong
dates, so 2008-02-30 becomes 200-03-01. That way:

$day_of_week = date('w',$unix_timestamp);

$year = date('Y', $unix_timestamp);
$month = date('n', $unix_timestamp);
$day = date('j', $unix_timestamp);
$start_of_week = mktime(0, 0, 0, $month, $day-$day_of_week, $year);

This (untested) code should work with American weeks (that start on
Sundays). Also, be aware of the peculiar argument order in mktime().


> I thought I could do this:
>
> $date_obj = date_create();
> date_date_set($date_obj,$year,$month,1);
>
> But I don't know how to get the timestamp out of the date_obj. What's
> the function for that?

I haven't worked with DateTime objects but I'd dare say you need good
old strtotime(). But you need to take into account that PHP timestamps
support is platform dependent: you'll face many issues if your scripts
need to run under Windows and handle dates out of the 1970-2038 range.


> Is there an easier way to do this?

For casual date handling, just encapsulate all this in your own custom
functions. For advanced date handling, you could consider third-party
classes like Pear's Date: http://pear.php.net/package/Date


> Perl is a bit erratic in how it indexes months and years. I didn't see
> much in the docs but:
>
> Is the month "1" indexed (ie, January is 1)?
> Is the day also 1 index?
> Is the year a 4 digit year?

Yes*, yes* and depends:

http://es2.php.net/manual/en/function.mktime.php
http://es2.php.net/manual/en/function.date.php

(*) As I said, mktime() will accept 2008-50-50 as valid date and fix it
as 2012-03-21, but 2008-0-0 becomes 2007-11-30.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--

0 new messages