Jump to content

[SOLVED] timestamp problem


Ekate

Recommended Posts

Hello, could you please advice me smth,'cause it works incorrect

 

<?php

$curdate=mktime(0,0,0,date("d"),date("m"),date("Y"));

  echo $curdate;

  $previousdate=$curdate - 2592000;

  echo $previousdate;

  $prdate = date("Y/m/d",$previousdate);

  echo $prdate;

?>

 

thank you

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Sorry, I really didn't specify what i wanted.

 

You're right,I need to find the date 30 prior to the current.

Thank you for your help.With your assistance it works.

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

 

Anyway,thank you

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.