-5

I have a user system for my website. I need to make something that adds 30 days to todays date.

UPDATE: I decided to go with an integer instead of a date.

Here's what I did. I set things up so that when someone pays their bill it sets their "Days" value to (value) + 30

I made a cron job that takes away 1 day from every user's "Days". Obviously, I set the cron job to run once per day.

Charles Harris
  • 17
  • 1
  • 2
  • 5

6 Answers6

9

Use strtotime and date as below.

echo date('Y-m-d', strtotime("+30 days"));

OR

<?php
   echo date('Y-m-d h:i:s')."\n";

   echo date('Y-m-d h:i:s', mktime(date('h'),date('i'),date('s'),date('m'),date('d')+30,date('Y')))."\n";

?>
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
3

First you can add date like this

$iSecsInDay = 86400;
$iTotalDays = 30;
$user_signup = time() + ($iSecsInDays * $iTotalDays);

Then you can use the timestamp to generate date in any way you want, like

$date = date('d-m-Y', $user_signup);

Full reference can be found here. http://php.net/manual/en/function.date.php

Prashank
  • 796
  • 6
  • 12
  • syntax error, unexpected T_VARIABLE, expecting T_FUNCTION – Charles Harris Mar 08 '13 at 08:03
  • I don't know how you are using it or where you are using it. To do the calculation you have to move this in a constructor or in a function probably. – Prashank Mar 08 '13 at 08:07
  • Basically what I'm doing is creating a variable called $user_signup that needs to = todays date plus 1 month. Which I then write into the mysql database using insert into blah blah blah. Make sense? – Charles Harris Mar 08 '13 at 08:12
  • Why don't you move the code in the function that is inserting and doing the mysql stuff. Why you want a constant? If you want a constant. Then the code needs to be changed. You want a constant to use in other places too? Yes/No? – Prashank Mar 08 '13 at 08:14
  • Hmm... I'll try moving it into the mysql function. – Charles Harris Mar 08 '13 at 08:18
  • don't forget to remove "private" keyword for that. – Prashank Mar 08 '13 at 08:19
  • Well, I tried that and it isn't working. Thanks for your help but I'm definitely going to need to research more on this one. I thought it would be an easy fix but it's not. – Charles Harris Mar 08 '13 at 08:24
  • Updated the code to make it more logical for usage in general case. I don't know why its not working for you. :( – Prashank Mar 08 '13 at 08:28
  • `$iSecsInDays` should be `$iSecsInDay` – Jason Ellis Jan 04 '21 at 04:23
2

Use strtotime function.

strtotime('+30 days', time());
2hamed
  • 8,719
  • 13
  • 69
  • 112
1

$NewDate=Date('y:m:d', strtotime("+30 days"));

strtodate() :

int strtotime ( string $time [, int $now = time() ] )

The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.

1

If you want to add +30 days to the actual date and introduce in mysql you could use:

$nextMonthDate = date("Y-m-d H:i:s", strtotime("+30 days",time()));

Y-m-d H:i:s is the format to MySQL DATETIME.

Example:

Actual date: echo echo date("Y-m-d H:i:s",time()); --> 2013-03-08 00:37:23

+30 days: echo date("Y-m-d H:i:s", strtotime("+30 days",time())); --> 2013-04-07 00:37:23

0

You cannot assign a function directly to a variable of a class. This is why you are getting the additional error. Consider moving it to your constructor.

... 
private $user_signup = null;

public function __construct() {
    $date = new DateTime();
    $date->add(new DateInterval('P30D'));
    $this->user_signup = $date->format('Y-m-d');
}
...

Also see Assigning a function's result to a variable within a PHP class? OOP Weirdness for more information.

Community
  • 1
  • 1
Thomas Hambach
  • 307
  • 2
  • 11