This question already has answers here:
Adding 30 minutes to time formatted as H:i in PHP
(7 answers)
Closed 9 years ago.
I have the following code
$time = "12:00";
$duration = 90 . " minutes";
$arrival = strtotime("+" . $duration, $time);
Output should be 13:30.
I get the following error: "A non well formed numeric value encountered in" (line with $arrival)
What can I do?
The following code should work:
$time = strtotime("12:00");
$duration = "+90 minutes";
$arrival = strtotime($duration, $time);
print(date("H:i", $arrival));
Demo: https://eval.in/95328
Read more about Unix timestamp and the PHP function strtotime.
Consider using PHP DateTime and DateInterval classes, like this:
$time = new \DateTime('now');
$time->add( new \DateInterval('PT90M') );
Try This Simple code :
<?php $time = strtotime('12:00');
$more = date("H:i", strtotime('+90 minutes', $time)); ?>
Related
This question already has answers here:
Adding one day to a date
(13 answers)
Closed 5 years ago.
I have a date which is:
$firstDay = "'" . date('m-d', easter_date(date('Y'))) . "'";
Output of $firstDay is: '04-16'. Now I need to create a second variable and add 1 day to the $firstDay variable. How should I do that? I red that I can do with $date->modify(), I'm trying like so:
$secondDay = $firstEasterDay->modify('+1 day');
But this doesn't work and I got an error.
Thanks for any help.
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);?>
Try This
This question already has answers here:
Why don't PHP and Javascript's timestamps match?
(3 answers)
Closed 6 years ago.
I have written the following code:
$timestamp = "1483625713000";
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
but the output is not coming as expected:
My outputs:
echo $date // 23-03-48984
echo $time // 2136.40
Looks like you've got milliseconds in that timestamp. Try this:
$timestamp = 1483625713000 / 1000;
$date = date('d-m-Y', $timestamp);
$time = date('Gi.s', $timestamp);
var_dump($date);
var_dump($time);
Outputs
string(10) "05-01-2017"
string(7) "1415.13"
This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
I have found several posts regarding this topic but I cannot get my function to work.
If the amount of days to add > 0, the returning date = 70-01-01
Example:
$date='12-07-11'
$days='5'
Should return 17-07-11 instead of 70-01-01
I need to use the variables because $date and $days are results from an oracle query.
function addDayswithdate($date,$days)
{
$originalDate = $date;
$newDate = date("y-m-d", strtotime($originalDate));
if ($days>0)
{
$var="+". $days ." days";
$date = strtotime($var, strtotime($newDate));
}
$originalDate = $date;
$newDate = date("y-m-d", strtotime($date));
return $newDate;
}
Use Datetime::createFromFormat and DateInterval
$date = DateTime::createFromFormat('d-m-y','12-07-11');
$date->add(new DateInterval('P5D'));
echo $date->format('d-m-Y') . "\n";
Why not keep it simpler?
function addDays($date, $days)
{
$time = strtotime($date);
$new_time = $time + (86400 * $days) //86400 seconds per day
return $new_time;
}
function addDays($date, $days)
{
$date_uts = strtotoime($date);
$add_days = $days*86400;
$final_date = date('Y-m-d', $date_uts+$add_days);
return $final_date;
}
Why not do it like this:
echo date('Y-m-d', strtotime($date + $days .'days'));
In addition of using vanilla php DateTime class, you can use Carbon if you have to work often with dates.
Carbon let you do anything like this
$date = Carbon::now()->addDays(5);
This question already has answers here:
Get timestamp of today and yesterday in php
(9 answers)
Closed 8 years ago.
I have
$today = date("Y-m-d");
and I like to define also $yesterday
I tried to do
$yesterday=($today-1)
But it doesn't work
Just try with:
$yesterday = date('Y-m-d', strtotime('yesterday'));
You could use something like:
date("F j, Y", time() - 60 * 60 * 24);
You can also use DateTime:
$date = new DateTime('');
$date->modify('-1 day');
echo $date->format('Y-m-d') . "\n";
This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
Using PHP how i can do this
$today = date("m/d/y"); // showing today
Output 11/18/13 will be saved in mysql as start date
Okay so if i like to add to this start date 30 days ... 60 days ... 90 days or any number of days to be add to $today and results be still in format m/d/y
Example may explain more what i mean
$today = date("m/d/y"); // 11/18/13
then add 10 days so $expired should be $today + 10; in days and results should be 11/28/13
~ thanks
Use DateTime::add() (PHP 5.3)
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
Try something like this should work for you
<?php
$Date = date("m/d/y");
echo date('m/d/y', strtotime($Date. ' + 10 days'));
?>
Use this code:
$today = date("m/d/y");
echo date('m/d/y', strtotime($today. ' +10 day'));
Out put will be 11/28/13 as you desiring.