I am trying to add 2 hours to $server_time and call it $hourPlusTwo. Everything i have tried ends up as either something like 7200 or some obscure date from 1969. How would you do it with what i have here, or rewritten a completely different way? Please understand i am new to php and programming in general. I am trying to understand how to do it, what would be better, and why it works.Thanks in advance.
date_default_timezone_set('America/Los_Angeles');
require_once('mysql_connection.php');
//analyse data by variable time period.
$hour_position = 45;
$htime = -30;
$date = date('Y-m-d H:i:s');
$date = strtotime($date);
$date = strtotime($htime." day", $date);
$date = date('Y-m-d H:i:s',$date);
$qry = "SELECT `last`,`server_time`,`vol` FROM `btce_btc_ticker` WHERE `server_time` > '$date' AND EXTRACT(MINUTE FROM `server_time`) = $hour_position ORDER BY `server_time` ASC";
$price_history_qry = mysqli_query($con,$qry);
while($result = mysqli_fetch_array($price_history_qry)){
$server_time = $result['server_time'];
$server_time = date("m-d-Y (H:i)",strtotime($server_time));
echo 'Server Hour ='.$server_time.'<br>';
echo 'Two Hour ='.$hourPlusTwo.'<br>';
}
You have your time format like this date('Y-m-d (H:i)
Whatever the time you got from the server you just need to speccify your format and add +2 hour from it.
The Change would be
$YourNewDate = date('Y-m-d (H:i)', strtotime('+2 hour'));
And the Result would be 2015-04-05 (23:05) some value like this format.
Update :
As you want to do the increment from the time you have from db
<?php
$result['server_time'] = '2014-04-18 19:56:00';
$server_time = $result['server_time'];
$hourPlusTwo = DateTime::createFromFormat("Y-m-d H:i:s", $server_time);
echo date('Y-m-d (H:i)', strtotime('+2 hours', $hourPlusTwo->getTimestamp()));
?>
Related
I'm trying to add two weeks to sql result that comes back as 16/11/2016. When I do something like
$twoweeks = strtotime($time_db);
$expiry_date = $twoweeks;
$date = strtotime($expiry_date);
$date = strtotime("+14 day", $date);
echo date('d/m/y', $date);
I keeping getting 15/01/70... any ideas?
You made a mistake on line 4 by putting a number variable as a first parameter of strtotime. strtotime expects a string of a valid date/time format as a first parameter otherwise it returns FALSE.
How I think your code should be:
$twoweeks = strtotime($time_db);
$date = strtotime("+ 2 weeks", $twoweeks);
echo date('d/m/y', $date);
Or maybe even:
echo date('d/m/y', strtotime($time_db . ' + 2 weeks'));
You can use
$numberOfWeeks = 2;
$newTime = strtotime($time_db) + ($numberOfWeeks * 60 * 60 * 24 * 7);
or you can do directly in (mysql) select
select date_add( your_column, INTERVAL 2 WEEK) from my_table;
What's happening here is that your 16/11/2016 is day-month-year and the slashes are an issue.
Had your date been 11/16/2016, you would have found that it would have been OK.
You need to convert/replace those to dashes/hyphens.
$time_db = "16/11/2016";
$time_db = str_replace('/', '-', $time_db);
$two_weeks_later = date('d-m-Y',strtotime($time_db . "+14 days"));
// or display as Year-month-day
// $two_weeks_later = date('Y-m-d',strtotime($time_db . "+14 days"));
echo $two_weeks_later;
When working with dates (and times), it's best to use the built-in MySQL date/time functions, rather than storing them as plain text; it's a lot less trouble and much easier when querying.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
$date = date('Y-m-d H:i:s',time());
$date = strtotime($date);
$date = strtotime("+14 day", $date);
$valuedate= date('Y-m-d H:i:s',$date);
Try this
I am trying to add hh:mm:ss with the date. How can i do it?
I tried with the following but it works when the hour is string, but when adding time is similar to MySQL Date time it is not working.
$new_time = date("Y-m-d H:i:s", strtotime('+5 hours'));
I am trying to get solution for the following:
$timeA= '2015-10-09 13:40:14';
$timeB = '03:05:01'; // '0000-00-00 03:05:01'
OutPut:
$timeA + $timeB = 2015-10-09 16:45:15 ?
How Can I Add this?
Use DateInterval():
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval('PT3H5M1S'); // '03:05:01';
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
You would need to break your time down into the right DateInterval format but that is easily done with explode();
Here's how that might look:
$parts = array_map(function($num) {
return (int) $num;
}, explode(':', '03:05:01'));
$timeA = new DateTime('2015-10-09 13:40:14');
$timeB = new DateInterval(sprintf('PT%uH%uM%uS', $parts[0], $parts[1], $parts[2]));
$timeA->add($timeB);
echo $timeA->format('Y-m-d H:i:s');
Demo
print date('Y-m-d H:i:s',strtotime($timeA." +03 hour +05 minutes +01 seconds"));
Should work also.
So:
$timeA= '2015-10-09 13:40:14';
$timeB = vsprintf(" +%d hours +%d minutes +%d seconds", explode(':', '03:05:01'));
print date('Y-m-d H:i:s',strtotime($timeA.$timeB));
Can be the solution.
You may also convert the time into seconds with this approach from: Convert time in HH:MM:SS format to seconds only?
$time = '03:05:01';
$seconds = strtotime("1970-01-01 $time UTC");
Then you could add the seconds to
$currentTime = '2015-10-10 13:40:14';
$newTime = date("Y-m-d H:i:s", strtotime( $currentTime.'+'.$seconds.' seconds'));
If you prefer to use the DateTime objects offered by #John Conde, here are two ways to convert the time string into the format:
$formattedTime = preg_replace("/(\d{2}):(\d{2}):(\d{2})/","PT$1H$2M$3S","03:05:11");
or, as you read it from the database:
select concat(hour(last_modified),'H',minute(last_modified),'M',second(last_modified),'H') from people;
So a more general code approach would be:
$initial = 'some time';
$interval = 'the interval value';
$initialTime = new DateTime($initial);
$intervalTime = new DateInterval($interval);
$initialTime->add($intervalTime);
echo $initialTime->format('Y-m-d H:i:s');
I have today date and i want to reduce hours\days from it. i get the "hours to reduce interval" in int that indicate number of days.
I tried something like this:
$today_date = date('Y-m-d H:i:s');
$temp_interval_date = $settings->days_back;
$interval_date = date('H',$temp_interval_date*24);
$final = $temp_interval_date - $interval_date;
My final goal is to get todaydate - interval period in this format
'Y-m-d H:i:s'
I am c# dude :)
Thanks
I'm not entirely clear on what you're asking but I think this is what you're looking for.
$date = new DateTime();
$date->sub(new DatePeriod('P'.$settings->days_back.'D'));
echo $date->format('Y-m-d H:i:s');
You can also do (if you're using PHP 5.2)
$date = new DateTime();
$date->modify('-' . $settings->days_back . ' days'));
echo $date->format('Y-m-d H:i:s');
reference
DateTime()
DatePeriod()
maybe this could be helpful:
echo date('Y-m-d', strtotime('-1 day', date('Y-m-d') ));
Another way to do it
<?php
$temp_interval_date = 2; //in hours (for example =2)
echo date('Y-m-d H:i:s', strtotime('-'.($temp_interval_date*24).' hours',strtotime(date('Y-m-d H:i:s'))));
?>
I would like to add 1 day and then subtract ( minus ) 1 second from a given time.
I did:
$fromDate = date("Y-m-d", strtotime("2012-09-28")).' 00:00:00';
$date = strtotime(date("y-m-d H:m:s", strtotime($fromDate)) . " +1 day") - 1;
$toDate = date('Y-m-d H:m:s', $date);
echo $toDate;
but instead of 2012-09-28 23:59:59 it returns 2012-09-29 00:09:59
What am I doing wrong?
You're going round and round instead of getting to the point in your code. Here's my solution with DateTime objects:
$time = new DateTime("2012-09-28");
$time->modify("+1 day");
$time->modify("-1 second");
var_dump($time);
Or, if you just need the last second of the day, why not just:
$time = "2012-09-28";
$time .= " 23:59:59";
As it's unlikely that the number of seconds/minutes/hours a day to change.
If I understand you right, you just want the last second in the given day, right?
If that's the case, then you could just have:
$theDate = "2012-09-28";
$fromDate = $theDate." 00:00:00";
$toDate = $theDate." 23:59:59";
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