Jump to content

[SOLVED] timestamp problem


Ekate

Recommended Posts

I'm not sure what you're after, but your code looks like it's trying to find the date 30 days prior to the current date. If so, you could use any number of options. Here are a couple to get you started:

<?php
// Option 1: strtotime()
$time = strtotime("today -30 day");
echo date('Y-m-d', $time);

// Option 2: time() or mktime()
$day   = 60 * 60 * 24;
$time  = time(); // or $time = mktime();
$time -= $day * 30;
echo date('Y-m-d', $time);
?>

 

If this isn't what you're after, try telling us what yours is actually showing and what you want it to show. That will help us debug with you.

Are you trying to get the previous month?

 

If so, just use mktime's ability to subtract...

 

echo "Today: " . date("Y/m/d") . "<br />";
echo "Last month: " . date("Y/m/d", mktime(0, 0, 0, date("m") - 1, date("d"), date("Y")));

 

Imagine it was the 1st month (ie January), then the month parameter would have 0 (which is not a valid month). Also, not all months got the same amount of days.

But I can't understand why my code won't work  :-\

 

I just realized you have your month and day switched in your mktime() call. That's why it's giving you a date sometime in 2009. Remember, though, with mktime(), it will default to the current date, so you don't have to provide the year, month and day at all:

<?php
$curdate = mktime(0,0,0,date('m'),date('d'),date('Y')); // notice the switch from yours
?>

Imagine it was the 1st month (ie January), then the month parameter would have 0 (which is not a valid month). Also, not all months got the same amount of days.

 

mktime is designed for such an occurrence.  Should that happen, it will go to the previous year's December...

 

http://us.php.net/mktime#id3107703

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.