Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

date_create

 
   Database Help (Home) -> PHP RSS
Next:  file conversion error  
Author Message
Jeff

External


Since: Jun 21, 2008
Posts: 128



(Msg. 1) Posted: Fri Oct 10, 2008 2:05 am
Post subject: date_create
Archived from groups: comp>lang>php (more info?)

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

 >> Stay informed about: date_create 
Back to top
Login to vote
Geoff Berrow1

External


Since: Dec 24, 2003
Posts: 359



(Msg. 2) Posted: Fri Oct 10, 2008 3:25 am
Post subject: Re: date_create [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Message-ID: <Q8Wdnd8W-L4lbXPVnZ2dnUVZ_uGdnZ2d.RemoveThis@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

 >> Stay informed about: date_create 
Back to top
Login to vote
Álvaro_G._Vicario

External


Since: Apr 11, 2008
Posts: 972



(Msg. 3) Posted: Fri Oct 10, 2008 4:25 am
Post subject: Re: date_create [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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 
Back to top
Login to vote
Display posts from previous:   
   Database Help (Home) -> PHP All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]