How to change a date via a variable

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
omegaman66
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Feb 25, 2019 2:40 am

Mon Feb 25, 2019 9:47 am

Hello,
Here is what I need to do.
User inputs a date into a text box.
User inputs other data via text box that I do some calculation on and end up with a number in a variable. This part is done.

What I want to do with these two variables (a $date and an amount of time) is add the amount of time ($extraTime) to the date.

So I am trying to add a variable that could contain something like 4.35 hours to a date and time t hat is held in another variable. So a simple x + y that is complicated with date:time data type.

I can easily convert the hours to seconds if needed so don't bother explaining that if the conversion is needed.

Thanks in advance from a newb.

omegaman66
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Feb 25, 2019 2:40 am

Mon Feb 25, 2019 2:03 pm

figured it out.

$var='3600'; //3600 seconds is 1 hour
$start = '2014-06-01 14:00:00';
$string= date(strtotime($start));
$stringplus=$string+$var;

echo date('l dS \o\f F Y h:i:s A', $stringplus);

chorn
php-forum Fan User
php-forum Fan User
Posts: 606
Joined: Fri Apr 01, 2016 2:18 am

Wed Feb 27, 2019 2:09 am

there's a shorter, more flexible and human readable way with the DateTime class, e.g.

Code: Select all

<?php

$date = new DateTime('2014-06-01 14:00:00');
print_r($date);

$date->modify('+1 hours');
print_r($date);

$date->modify('+12 minutes');
print_r($date);

$date->modify('+123 years');
print_r($date);

omegaman66
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Feb 25, 2019 2:40 am

Thu Mar 07, 2019 10:23 am

Thanks!

Post Reply