How can i add days to time stamp - php

How can i add days to time stamp and echo the time.
I tried and able to add days to it but when echo it shows a no's.
$timestamp = strtotime('+7 days', $row['sdate']);
Is this code is correct.
Thankyou

It should be like
$date = strtotime($row['sdate']);
$date = strtotime("+7 day", $date);
echo date('Y-m-d H:i:s',$date);
OR you can also try like
$date = strtotime('+7 days', strtotime($row['sdate']) );
echo date('Y-m-d',$date);

Strtotime function wants unix timestamp, so you need to convert your time (that i expect is normal datetime)?
strtotime('+7 days', strtotime($row['sdate']) );
Then if you want to format it to datetime again you do like this:
$timestamp = strtotime('+7 days', strtotime($row['sdate']) );
echo date("Y-m-d H:i:s", $timestamp);
You can read more about strtotime and date on php's manual.
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.date.php

Related

Obtain 1 Hour Before Without Minutes/Seconds [duplicate]

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));

Strtime returning wrong date time

I have this simple PHP function:
<?php
$start_date = strtotime('-7 days', '2014-06-04 00:00:00');
echo date('Y-m-d H:i:s', $start_date);
?>
This returns 1969-12-24 18:33:34. Is there anything wrong in the code?
Make the second argument Unix Timestamp.
$start_date = strtotime('-7 days', strtotime('2014-06-04 00:00:00'));
In fact you can also get away with
$start_date = strtotime('2014-06-04 00:00:00 -7 days');

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 do I return the previous Sunday from 7 days ago using PHP date()?

Here is what I have so far:
$date = date('Y-m-d h:i:s', strtotime('-7 days'));
$start = date('Y-m-d h:i:s', strtotime($date,'previous Sunday'));
When outputting $start, it returns: 1969-12-31 06:00:00
What am I doing wrong?
$date needs to e a timestamp
$date = strtotime('-7 days');
$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));
You have the arguments the wrong way round:
date('Y-m-d h:i:s', strtotime('previous Sunday', $date));
Edit: Furthermore, you have made $date a formatted string. It needs to be a timestamp, so your code should look something like this:
$date = strtotime('-7 days');
$start = date('Y-m-d h:i:s', strtotime('previous Sunday', $date));
Per php doc
date('Y-m-d h:i:s', strtotime('last Sunday', $date));
If your date is not a timestamp you can still use strtotime, like suppose your date was passed in already and is in a string format of another kind.
$date = '2013-11-10';
$lastsunday = date('Y-m-d',strtotime($date.' last Sunday'));
This can save a bit of time trying to get your date into a format that "works"

Categories