Date problem.

O

ober

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.
 
D

David A. Black

Hi --

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
 
J

Joe Cheng

ober said:
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

Joe Van Dyk

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
 
D

David A. Black

Hi --

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 :)

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
 
W

William James

ober said:
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 ]
 
R

Randy Kramer

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
 
L

Logan Capaldo

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

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>
 
J

Joe Van Dyk

=20
Huh, never thought there might be a man page for ruby. Interesting, wort= h
reading (and now singleton methods might click into place).
=20
And there's man pages for perl, python, c++ (cpp), and probably others, b= ut no
c (or C or cee). Interesting!

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)
 
R

Randy Kramer

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)

Thanks!

Randy Kramer
 
R

Randy Kramer

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>

Thanks!

Randy Kramer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,177
Messages
2,570,954
Members
47,507
Latest member
codeguru31

Latest Threads

Top