Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
Related
I have a string in the format YYYYMMDDHH24MISS that is year, month, day, hours, minutes, seconds. I want to convert this to a date, add one day to it and return it in the same format. Sounds simple but I am unable to get this to work. I have tried a number of different ways where $field3 contains the date string for example:
$end_date = strtotime(substr($field3,1,8));
$date_interval = DateInterval::createFromDateString('1 day');
$new_end_date = date_add($end_date, $date_interval);
$field3 = ($new_end_date->format('YYYYMMDD')).substr($field3,8,6);
In this example $new_end_date contains "false".
Example date time string: 20170912124159 being 12/09/2017 12:41:59
The format of your input string can be parsed by the constructor of class DateTime (and date_create() and strtotime()) without problems.
$date = new DateTime('20170912124159');
$date->add(new DateInterval('P1D'));
echo($date->format('Y-m-d H:i:s'));
# The output is:
# 2017-09-13 12:41:59
You can, as well, format the date as string using the format YmdHis to get the modified date in the same format as the input string.
echo($date->format('YmdHis'));
# 20170913124159
Read about DateTime and DateInterval.
You can try something like this:
$date = DateTime::createFromFormat('YmdHis', '20170912131313');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d');
For more information: http://php.net/manual/en/datetime.add.php
Please try this
$date = date("Y/m/d H:i:s"); // or '2017/09/30 20:24:00'
$ndate = date('Y/m/d H:i:s', strtotime($date . ' +1 day'));
echo 'date after adding 1 day: ' . $ndate;
I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));
I'm trying to extract just the day of the month from the dateTime formatted as follows:
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
But, when I try extracting just the day of the month, I get '31'.
I'm using: $day = date('d', $today);
Which, I'm guessing, is incorrect.
This is because the second parameter to date() needs to be a Unix Timestamp. You're passing it string. As a result you get a date of Dec 31, 1969.
All of that code is unnecessary anyways as all you need is:
$day = day('d');
If you're going to only have access to the date string you must convert it to Unix Timestamp before passing it to date().
To extract days (or other parts of a datetime), I use the format function:
$datetime = new DateTime('2000-01-10', new DateTimeZone('Pacific/Nauru'));
$day = $datetime->format('d');
echo $day;
Use any format form the PHP Manual.
Hope this will solve your problem
$today = date('Y-m-d\TH:i:sP', strtotime('today'));
$day = date('d', strtotime($today)); // here is the difference,
// instead of $today use strtotime($today)
I am trying to add minutes to current date but it returns strange results
date_default_timezone_set('Asia/Karachi');
$currentDate = date("m-d-Y H:i:s");
$currentDate_timestamp = strtotime($currentDate);
$endDate_months = strtotime("+10 minutes", $currentDate_timestamp);
$packageEndDate = date("m-d-Y H:i:s", $endDate_months);
echo " <br> " . $packageEndDate . " <br> ";
echo $currentDate;
I am getting Output
01-01-1970 05:50:00
07-19-2013 20:25:23
It should return
07-19-2013 20:35:23
07-19-2013 20:25:23
After this I need to query to database so date format should be same. Database column is of string type.
Your code is redundant. Why format a timestamp as a string, then convert that string back to a timestamp?
Try
$now = time();
$ten_minutes = $now + (10 * 60);
$startDate = date('m-d-Y H:i:s', $now);
$endDate = date('m-d-Y H:i:s', $ten_minutes);
instead.
Probably the minimalist way would be:
date_default_timezone_set('Asia/Baku');
$packageEndDate = date('Y-m-d H:i:s', strtotime('+10 minute'));
echo $packageEndDate;
Output (Current time in my city at the time of writing):
2017-07-20 12:45:17
Try this:
$now = time();
$tenMinFromNow = date("m-d-Y H:i:s", strtotime('+10 minutes', $time));
$tenMinsFromNow = (new \DateTime())->add(new \DateInterval('PT10M'));
Will leave you with a DateTime object representing a time 10 minutes in the future. Which will allow you to do something like:-
echo $tenMinsFromNow->format('d/m/Y H:i:s');
See it working
PHP version >= 5.4 I'm afraid, but you should be using at least that version by now anyway.
Pakistan, which is the localisation explicitly set, uses "DD-MM-YYYY" format dates so the problem occurs when you cast the date into a string of "MM-DD-YYYY". This American format of date is not parseable by the Pakistan localisation.
If you still want to keep the round-trip to a string and back, use DD-MM-YYYY or the ISO datetime format.
While this is the only (current) answer which actually explains your original issue, I recommend the code be refactored as others have demonstrated.
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');