strtotime minus hours and minutes - php

I have this code:
<?php
$date = date('Y-m-d H:i:s', strtotime('-6 hours', (int)get_the_time('U')));
echo $date;
?>
as you can see the time is changed by subtracting 6 hours from the time, what I want to subtract the hours and also minutes so I tried this:
<?php
$date = date('Y-m-d H:i:s', strtotime('-6 hours', '-23 minutes', (int)get_the_time('U')));
echo $date;
?>
But isn't working, any idea?
Thank you in advance

The strtotime function expecting only 2 parameters.
int strtotime ( string $time [, int $now = time() ] )
So you should try the following format:
$date = strtotime('-6 hours, -43 minutes', $time);
Tested Code:
<?php
$time = time();
$date = strtotime('-6 hours', $time);
echo date('d-m-y H:i', $time);
echo date('d-m-y H:i', $date);
$date = strtotime('-6 hours, -43 minutes', $time);
echo date('d-m-y H:i', $time);
echo date('d-m-y H:i', $date);
Output:
19-10-17 00:44
18-10-17 18:44
19-10-17 00:44
18-10-17 18:01

$time=time();
$datetime_from = date("Y-m-d H:i", strtotime("-43 minutes", strtotime("-6 hours",$time)));

Related

How to show next month in php

$date ="2018-02-15";
$next_month = date('$date', strtotime('+1 month');
echo "$next_month";
I want the output as "2018-03-15";
But this code not working.
You can use strtotime
$date ="2018-02-15";
$time = strtotime($date);
$next_month = date("Y-m-d", strtotime("+1 month", $time));
echo $next_month;
The result will be:
2018-03-15
Doc: strtotime
use this:
$date ="2018-04-22";
$next_month = date('Y-m-d', strtotime($date.' +1 month'));
echo "$next_month";

DateTime 7 days from now at 00:00

I'm trying to get the date 1 week before at 00:00:00 Here's what I've tried
$now = date("Y-m-d H:i:s");
$start_date_time = date('Y-m-d H:i:s', strtotime("-7 day"));
Output is 2017-04-11 11:33:52 (UTC)
I tried to use
$start_date_time ->setTime(0, 0);
echo $start_date_time ->format('H:i:s');
But it's not datetime so I get an error. Any tips?
you could use strtotime
$start_date_time = date('Y-m-d', strtotime('-7 days'));
or DateTime class
$date = new DateTime('7 days ago');
$date ->format('Y-m-d');
Just use DateTime and DateInterval.
Example:
$date = new DateTime();
$date->sub(new \DateInterval('P7D'));
echo $date->format('Y-m-d 00:00:00');
$d=strtotime("-7 Days");
echo date("Y-m-d 00:00:00", $d);

How to add mintues to date using php?

I want to add 30 minutes to the current date. But if i do so, it displays 1970-01-01 01:03:33(Unix timestamp).How to retrieve the result in a format that strtotime() parser understands?
Here is my code:
$date1 = date("Y-m-d H:i:s");
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', $date1));
That's because date() returns a string, and you are adding 30 minutes to a string instead of a date.
Try this:
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', now()));
or
$date1 = date("Y-m-d H:i:s");
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', strtotime($date1)));
This should be the answer
echo date("Y/m/d h:i:s", strtotime("+30 minutes"));
strtotime expects a timestamp as its second parameter, not a formatted date. But in any case, you can just work with timestamps:
$newdate = date("Y-m-d H:i:s", time()+30*60);
Try This
$curDate = date('Y-m-d', strtotime("+30 minutes", strtotime(date('Y-m-d'))));
$now = time();
$later = $now + 60*30;

how do I subtract 24 hour from date time object in PHP

I have the following code:
$now = date("Y-m-d H:m:s");
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
However, now it gives me this error:
A non well formed numeric value encountered in...
why is this?
$date = (new \DateTime())->modify('-24 hours');
or
$date = (new \DateTime())->modify('-1 day');
(The latter takes into account this comment as it is a valid point.)
Should work fine for you here. See http://PHP.net/datetime
$date will be an instance of DateTime, a real DateTime object.
strtotime() expects a unix timestamp (which is number seconds since Jan 01 1970)
$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify.
i would suggest using the datetime library though, since it's a more object oriented approach.
$date = new DateTime(); //date & time of right now. (Like time())
$date->sub(new DateInterval('P1D')); //subtract period of 1 day
The advantage of this is that you can reuse the DateInterval:
$date = new DateTime(); //date & time of right now. (Like time())
$oneDayPeriod = new DateInterval('P1D'); //period of 1 day
$date->sub($oneDayPeriod);
$date->sub($oneDayPeriod); //2 days are subtracted.
$date2 = new DateTime();
$date2->sub($oneDayPeriod); //can use the same period, multiple times.
Carbon (update 2020)
Most popular library for processing DateTimes in PHP is Carbon.
Here you would simply do:
$yesterday = Carbon::now()->subDay();
you can do this in many ways...
echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros
OR
echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)
Output
2013-07-17 10:07:29
Simplest way to sub or add time,
<?php
**#Subtract 24 hours**
$dtSub = new DateTime('- 24 hours');
var_dump($dtSub->format('Y-m-d H:m:s'));
**#Add 24 hours**
$dtAdd = new DateTime('24 hours');
var_dump($dtAdd->format('Y-m-d H:m:s'));die;
?>
This may be helpful for you:
//calculate like this
$date = date("Y-m-d H:m:s", (time()-(60*60*24)));
//check the date
echo $date;
this should work, too
$date = date("Y-m-d H:m:s", strtotime('-24 hours'));
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));
Add "strtotime" before $now,
and Y-m-d H:m:s replace with Y-m-d H:i:s
You can simply use time() to get the current timestamp.
$date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
In same code use strtotime() its working.
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-2 hours', strtotime($now)));
Try this :
$now = time();
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
all you have to do is to alter your code to be
$now = strtotime(date("Y-m-d H:m:s"));
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));

How to add 5 minutes to current datetime on php < 5.3

I want to add 5 minutes to this date: 2011-04-8 08:29:49
$date = '2011-04-8 08:29:49';
When I use strtotime I am always getting 1970-01-01 08:33:31
How do I add correctly 5 minutes to 2011-04-8 08:29:49?
$date = '2011-04-8 08:29:49';
$currentDate = strtotime($date);
$futureDate = $currentDate+(60*5);
$formatDate = date("Y-m-d H:i:s", $futureDate);
Now, the result is 2011-04-08 08:34:49 and is stored inside $formatDate
Enjoy! :)
Try this:
echo date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime('2011-04-8 08:29:49')));
$expire_stamp = date('Y-m-d H:i:s', strtotime("+5 min"));
$now_stamp = date("Y-m-d H:i:s");
echo "Right now: " . $now_stamp;
echo "5 minutes from right now: " . $expire_stamp;
Results in:
2012-09-30 09:00:03
2012-09-30 09:05:03
$date = '2011-04-8 08:29:49';
$newDate = date("Y-m-d H:i:s",strtotime($date." +5 minutes"))
For adding
$date = new DateTime('2014-02-20 14:20:00');
$date->add(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It add 5minutes
For subtracting
$date = new DateTime('2014-02-20 14:20:00');
$date->sub(new DateInterval('P0DT0H5M0S'));
echo $date->format('Y-m-d H:i:s');
It subtract 5 minutes
If i'm right in thinking.
If you convert your date to a unix timestamp via strtotime(), then just add 300 (5min * 60 seconds) to that number.
$timestamp = strtotime($date) + (5*60)
Hope this helps
more illustrative for simple and clear solution
$date = '2011-04-8 08:29:49';
$newtimestamp = strtotime($date. ' + 5 minute');//gets timestamp
//convert into whichever format you need
$newdate = date('Y-m-d H:i:s', $newtimestamp);//it prints 2011-04-08 08:34:49

Categories