Adding 1 Day to a Datetime Format Value

adding 1 day to a DATETIME format value

If you want to do this in PHP:

// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));

If you want to add the date in MySQL:

-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);

DateTime add 1 day

you can use Add function of Datatime object

here i am giving you one example to add one 1 in your post date like

<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P1D'));
echo $date->format('Y-m-d') . "\n";
?>

OUTPUT

2000-01-02

Hope it will sure help you to solve your issue.

Adding one day to a date

<?php
$stop_date = '2009-09-30 20:24:00';
echo 'date before day adding: ' . $stop_date;
$stop_date = date('Y-m-d H:i:s', strtotime($stop_date . ' +1 day'));
echo 'date after adding 1 day: ' . $stop_date;
?>

For PHP 5.2.0+, you may also do as follows:

$stop_date = new DateTime('2009-09-30 20:24:00');
echo 'date before day adding: ' . $stop_date->format('Y-m-d H:i:s');
$stop_date->modify('+1 day');
echo 'date after adding 1 day: ' . $stop_date->format('Y-m-d H:i:s');

Function to add 1 day to date using php

This should work:

$date = "2018-07-18";
$new_date = date('Y-m-d', strtotime($date. ' + 1 days'));

Add +1 or more day to DATETIME in php eaven if that day is in a different month

$date = "2017-12-31";

echo date("Y-m-d", strtotime($date . " +5 days"));

Strtotime() can translate some sentences to actions. See example 1

Adding days to a date in Python

The previous answers are correct but it's generally a better practice to do:

import datetime

Then you'll have, using datetime.timedelta:

date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")

end_date = date_1 + datetime.timedelta(days=10)

Add +1 day to each value in the column

You need to convert your string into a date object.
Once your string is a date object, you can apply the time delta and then turn the new date object back into a string.

#Assuming datetickerdf['date']  is something like 21 June, 2018
#My Delta
aday = dt.timedelta(days=1)
#The date in the file converted to a date object
fileday = dt.datetime.strptime(datetickerdf['date'], "%d %B, %Y")
#New date calculated with delta
newday = fileday+aday
#Write it back to a file
datetickerdf['date'] = newday.strftime("%d %B, %Y")

Use this page to get the right format string to parse and rewrite the string https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

PHP: DateTime '-1 day'

This problem, according to the documentation for the modify() method, seems to entirely depend on which version of php you're using. In this case, method chaining(which is what you're attempting to do is called), is only available on php version 5.3.0 and up, according to the changelog on the previously linked docs.

That in mind, it explains why your code didn't work, and @Deryck's did. If you ever do upgrade your php version, or get your host to upgrade it, you could likely reduce those three lines to two:

$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake')); 
$date = $date->modify( '-1 day' )->format('Y-m-d');

Not much of an improvement, I realize, but there's your reason for why it failed to work.

Below are two of the methods I see of getting around this; one is creation of a class.. which seems like overkill to me unless this is apart of something grander... the other is a creation of a function. Both shove the extra lines into something that takes up less space, in a sense.

class DT {
private $now; //can be null
private $timezone; //DateTimeZone object
public function __construct($tz_str, $now = NULL) {
$this->now = $now;
$this->timezone = new DateTimeZone($tz_str);;
}

public function mod($mod,$format) {
$dt = new DateTime($this->now, $this->timezone);
$dt->modify($mod);
return $dt->format($format);
}
}

function mod_formatted_zone($tz_str, $mod, $format, $now = NULL) {
$timezone = new DateTimeZone($tz_str);
$dt = new DateTime($now,$timezone);
$dt->modify($mod);
return $dt->format($format);
}

The use of either is simple; in the case of the class, it'd be something like..

$dt = new DT('Pacific/Wake');
echo $dt->mod('-1 day', 'Y-m-d');

While in the case of the function, it'd simply be..

echo mod_formatted_zone('Pacific/Wake', '-1 day', 'Y-m-d');

Add 1 day to my date in Python

You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:

>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'

C# date format over 1 day

For DateTime, day is the day of the month. What you want is a TimeSpan.

var span = TimeSpan.FromSeconds(80000);
Console.WriteLine(span.ToString(@"d'd 'h'h 'm'min'"));

outputs

0d 22h 13min



Related Topics



Leave a reply



Submit