Strtime returning wrong date time - php

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

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

How can i add days to time stamp

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

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"

adding 1 day to a DATETIME format value

In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
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);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');

Categories