Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

   I'm porting some perl (a calendar) over to php and I have some date

   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();

  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!


Re: date_create

contained the following:

Quoted text here. Click to load it
Geoff Berrow  0110001001101100010000000110
100110001101101111001011100111010101101011 -

Re: date_create

Jeff escribió:
Quoted text here. Click to load it

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().

Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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:

Quoted text here. Click to load it

Yes*, yes* and depends:

(*) 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.

-- - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web:
-- Mi web de humor al baño María:

Site Timeline