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
--
>> Stay informed about: date_create