Date problem

(Jaime Fournier) #1

I have read over the man pages and can not seem to find a way to do the following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

Thanks in advance.

(David A. Black) #2

Hi --

···

On Tue, 30 Aug 2005, ober wrote:

I have read over the man pages and can not seem to find a way to do the following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

require 'date'
d = Date.today
puts Date.new(d.year, d.month-1, 1).strftime("%Y-%m-%d")

David

--
David A. Black
dblack@wobblini.net

(Joe Cheng) #3

ober wrote:

I have read over the man pages and can not seem to find a way to do the following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

A literal translation to Ruby:
lastmonth = Date.new(Date.today.year, Date.today.month - 1)
name = lastmonth.to_s
puts name

However, this will give you the wrong answer in January. Here's one that accounts for January:

year, lastmonth = Date.today.year, Date.today.month - 1
year, lastmonth = year - 1, 12 if lastmonth == 0
puts Date.new(year, lastmonth).to_s

(J-Van) #4

There's gotta be a better way of doing this, but:

irb(main):001:0> require 'date'
irb(main):002:0> puts Date.civil(2005, (DateTime.now.month) -1, 1)
2005-07-01

···

On 8/29/05, ober <ober@linbsd.org> wrote:

I have read over the man pages and can not seem to find a way to do the
following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

Thanks in advance.

(W. James) #5

ober wrote:

I have read over the man pages and can not seem to find a way to do the
following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

If you don't want to require 'Date':

month,year = Time.now.to_a[4..5]
month -= 1
month, year = month+12, year-1 if month < 1
puts "%d-%02d-01" % [ year, month ]

(David A. Black) #6

Hi --

ober wrote:

I have read over the man pages and can not seem to find a way to do the following

-----php---------------
$lastmonth = mktime(0, 0, 0, date("m")-1, date(01), date("Y"));
$name = date('Y-m-d', $lastmonth);
echo "$name\n";
----php----------------

Where the first day of the previous month is listed.
In this case you would get 2005-07-01

A literal translation to Ruby:
lastmonth = Date.new(Date.today.year, Date.today.month - 1)
name = lastmonth.to_s
puts name

However, this will give you the wrong answer in January. Here's one that accounts for January:

year, lastmonth = Date.today.year, Date.today.month - 1
year, lastmonth = year - 1, 12 if lastmonth == 0
puts Date.new(year, lastmonth).to_s

I think my answer probably suffered from the January bug. Here's
another way, addressing that problem in a slightly different way from
yours (less work :slight_smile:

   irb(main):035:0> d = Date.today << 1
   => #<Date: 4907161/2,0,2299161>
   irb(main):036:0> Date.new(d.year, d.month, 1).to_s
   => "2005-07-01"

January test:

   irb(main):037:0> d = Date.new(2005,1,15) << 1
   => #<Date: 4906709/2,0,2299161>
   irb(main):038:0> Date.new(d.year, d.month, 1).to_s
   => "2004-12-01"

David

···

On Tue, 30 Aug 2005, Joe Cheng wrote:

--
David A. Black
dblack@wobblini.net

(Randy Kramer) #7

Huh, never thought there might be a man page for ruby. Interesting, worth
reading (and now singleton methods might click into place).

And there's man pages for perl, python, c++ (cpp), and probably others, but no
c (or C or cee). Interesting!

Randy Kramer

···

On Monday 29 August 2005 12:55 pm, Joe Cheng wrote:

ober wrote:
> I have read over the man pages and can not seem to find a way to do
> the following

(Logan Capaldo) #8

Try man cc or man gcc (if on a system with GNU tools). That probably isn't what you are looking for though, however MANY of the man pages on a system are about the standard C library and the syscalls of the OS you are on. eg. man 3 exec on a BSD system will bring up the documentation for the exec family of functions. It may be a different section on other *nixes, I seem to recall that stuff being in (2) on Linux, but I could be wrong. Try man <some C library function>

···

On Aug 29, 2005, at 3:18 PM, Randy Kramer wrote:

On Monday 29 August 2005 12:55 pm, Joe Cheng wrote:

ober wrote:

I have read over the man pages and can not seem to find a way to do
the following

Huh, never thought there might be a man page for ruby. Interesting, worth
reading (and now singleton methods might click into place).

And there's man pages for perl, python, c++ (cpp), and probably others, but no
c (or C or cee). Interesting!

Randy Kramer

(J-Van) #9

The man page for c++ is for g++, the GNU GCC compiler. There is no
"c" or "c++" program, so there's no man pages for it. (there's a
perl, python, and ruby program, however, so there's man pages for
them)

···

On 8/29/05, Randy Kramer <rhkramer@gmail.com> wrote:

On Monday 29 August 2005 12:55 pm, Joe Cheng wrote:
> ober wrote:
> > I have read over the man pages and can not seem to find a way to do
> > the following

Huh, never thought there might be a man page for ruby. Interesting, worth
reading (and now singleton methods might click into place).

And there's man pages for perl, python, c++ (cpp), and probably others, but no
c (or C or cee). Interesting!

(Randy Kramer) #10

Thanks!

Randy Kramer

···

On Monday 29 August 2005 03:55 pm, Joe Van Dyk wrote:

The man page for c++ is for g++, the GNU GCC compiler. There is no
"c" or "c++" program, so there's no man pages for it. (there's a
perl, python, and ruby program, however, so there's man pages for
them)

(Randy Kramer) #11

Thanks!

Randy Kramer

···

On Monday 29 August 2005 03:33 pm, Logan Capaldo wrote:

Try man cc or man gcc (if on a system with GNU tools). That probably
isn't what you are looking for though, however MANY of the man pages
on a system are about the standard C library and the syscalls of the
OS you are on. eg. man 3 exec on a BSD system will bring up the
documentation for the exec family of functions. It may be a different
section on other *nixes, I seem to recall that stuff being in (2) on
Linux, but I could be wrong. Try man <some C library function>