How to set an specific date? - php

I'm trying to create an date with
$date_end = mktime(0, 0, 0, date('m'), date('d')+7, date('Y'), $date_set);
The output is today + 7 days instead of the date given + 7.

The manual says nothing about mktime() taking a date as argument.
Use strtotime("+7 days", $date_set).

$date_end = mktime(0, 0, 0, date('m', $date_set), date('d', $date_set)+7, date('Y', $date_set));
is, I believe, what you were trying to accomplish (assuming $date_set is a timestamp). Else, #Kristian's suggestion I believe is a good one.

Why are you passing a $date_set variable, and why are you using mktime if you already have the time?
Simply add 7 days: $date_end = $date_set + (7 * 86400);

Related

Changing Year Specifically in date()

I wish to do an operation on a given date string which needs to change the year of that date to, let's say, 2000. Is there a way to accomplish this feat using PHP's date()?
$old_date = '2017-05-05';
// Needs to change 2017 to 2000.
$new_date = ?
I know one method involves using strtotime($old_date. '-17 years') but its not applicable in every case, such as 2018 which will become 2001. Is there an alternate solution that avoids hardcoding the number of years to subtract?
NOTE: I'm assigning a date string to $old_date for convenience. The actual date is fetched from a table.
Subtract years after millennium
strtotime($old_date . ' -' . date('y', strtotime($old_date)) . 'years')
$time = strtotime($old_date);
$year = 2000;
$new_date = date("Y-m-d", mktime(0, 0, 0, date('n', $time), date('j', $time), $year));

Input a date and give a date in the future

I am letting a user input a date. How should I return a date from their input and add 3 years to it? I tried to use mktime and date but it did not work out very well.
$input_date = 2010-03-28
My solution currently is just basic math for a given date 3 years ahead.
$input_date = $input_date + 3000;
Let's say I would want to give a date 4 years and 4 months 10 days
$future_date1 _datum = mktime(0, 0, 0, date("m")-2, date("d"), date("Y")+3);
$future_date2 = mktime(0, 0, 0, date("m"), date("d"), date("Y")+3);
You can use strtotime('+ 3 years') or DateInterval for an object oriented approach.
Use the strtotime function:
$input_date = '2010-03-28';
$future_date = strtotime("+3 years", strtotime($input_date);
http://www.php.net/manual/en/function.strtotime.php
Returns a timestamp, if you want to return YYYY-mm-dd: $future_date = date("Y-m-d", $future_date);
DateTime() offers multiple ways to do this. It's the way PHP recommends doing date math.
You can use DateTime::modify():
$date = new DateTime($input_date);
$date->modify('+3 months');
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->modify('+3 months')->format('Y-m-d');
or with DateInterval()
$date = new DateTime($input_date);
$date->add(new DateInterval('P3M'));
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->add(new DateInterval('P3M'))->format('Y-m-d');

Get current date and time form mktime in PHP

I have a problem with find the current date from past mktime. In PHP I find the current date using date("j");. Here I need, suppose my date was in the past year like mktime(0, 0, 0, 2, 1, 2008), then here how can I find the current date of this particular past month.
Either as #octern's solution, or you can do
$day = date('j', strtotime("-2 months"));
or
$day = date('j', strtotime('-30 days'));
depending on your need.
You may also want to refer to strtotime() manual.
Try this:
$date = getdate(mktime(0, 0, 0, 2, 1, 2008));
$day = $date['mday'];
Or just:
$day = date('j', mktime(0, 0, 0, 2, 1, 2008))

PHP date() - inserting into an MySQL database

I'm having a little bit of trouble with getting an incremented date into an MySQL database. The field is of type DATE.
First of all I increment the date with the following code:
if($_POST['membershipLength'] == "6 Months") {
$renew = mktime(0, 0, 0, date("m")+9, date("d"), date("y"));
}
Then I wish to enter: date("m/d/y", $renew) into the column in the database.
Can anyone see where I'm going wrong? The value it inserts consists of 0s which I'm assuming is the default.
The MYSQL date field has the following format "Y-m-d" so something like "2010-11-04". You're trying to insert a value as "2010/04/11". Change your first parameter to "Y-m-d" and it should work :)
Try this:
date ("Y-m-d H:i:s", $renew);
MySQL likes dates in a specific format.
the date format in MySQL is YYYY-MM-DD and you are inserting M-D-Y try with Y-M-D it will resolve your problem
I tried your code and working fine, other than one thing. When you want to add 6 months, why are you adding 9 in months?
if($_POST['membershipLength'] == "6 Months") {
$date = mktime(0, 0, 0, date("m")+6, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
} elseif($_POST['membershipLength'] == "9 Months") {
$date = mktime(0, 0, 0, date("m")+9, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
} elseif($_POST['membershipLength'] == "12 Months") {
$date = mktime(0, 0, 0, date("m")+12, date("d"), date("Y"));
$renew = date("Y-m-d", $date);
}
Thanks for your help everyone. The mistake I was making in addition the corrections you all suggested was that I was checking for a value that didn't match. Silly me!

php date functions

I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples

Categories