how to get half DateTime between two dates? - php

i have current datetime and delivery datetime
$del_time = $this->input->post('bid_time');
date_default_timezone_set('Asia/Kolkata');
$now = date('Y-m-d H:i:s');
i want exact half of datetime in between current datetime and delivery datetime in Y-m-d H:i:s period in php

You can use following code
date_default_timezone_set('Asia/Kolkata');
$del_time = $this->input->post('bid_time'); //for ex '2016-12-01 12:30:00';
$now = date('Y-m-d H:i:s');
$loc_del_date_time = strtotime($del_time);
$loc_curr_date_time = strtotime($now);
$loc_mid_time = ($loc_del_date_time + $loc_curr_date_time) / 2;
$loc_mid_time = round($loc_mid_time);
echo $loc_new_date = date('Y-m-d H:i:s', $loc_mid_time);
Thanks

Related

Convert an Unix timestamp to good GMT date PHP

I want to get the FRENCH GMT DATE from an Unix timestamp.
In my database, i saved the date in GMT+0 then i get the timestamp of this date and want to display the date with the good GMT+2
My timestamp is :1461857633 from database and it's equal to : 29/04/2016 12:27:11
And now i want to display this date with local GMT.
So i did this :
$timestamp = 1461857633;
$format = 'd/m/Y H:i:s';
$res = date($format, $timestamp);
echo $res;
and i have the same date 29/04/2016 12:27:11 where as my timezone is well 'Europe/Paris'
Normaly i should to have this date : 29/04/2016 14:27:11
$timestamp = 1461857633;
$effectiveDate = strtotime("+120 minutes", $timestamp);
$format = 'd/m/Y H:i:s';
$res = date($format, $effectiveDate);
echo $res;
date_default_timezone_set("UTC");
$HUTC = date("h");
date_default_timezone_set("Europe/Paris");
$HParis = date("h");
$diff = $HParis - $HUTC;
$timestamp = 1461857633;
$timestamp = 1461857633 + $diff * 60 * 60;
$format = 'd/m/Y H:i:s';
$res = date($format,$timestamp);
echo $res;
This will work both summer and winter time
You can simply add 2 hours to the timestamp or you can create a DateTime object and modify it by adding 2 hours to it:
$timestamp = 1461857633 + 2 * 60 * 60;
or
$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633)->modify('+2 hours');
echo $dateTime->format('d/m/Y H:i:s');
or, another solution would be to calculate the seconds between the timezone you want to convert to and the Greenwich time zone (which is GMT+0) like:
$greenwichTimeZone = new DateTimeZone('Greenwich Mean Time');
$parisTimeZone = new DateTimeZone('Europe/Paris');
$dateTimeGreenwich = new DateTime('now', $greenwichTimeZone);
$seconds = $parisTimeZone->getOffset($dateTimeGreenwich);
$dateTime = new DateTime();
$dateTime->setTimestamp(1461857633 + $seconds);
echo $dateTime->format('d/m/Y H:i:s');

Subtract minutes from date using strtotime

I have the current date format $date = 'yyyy-mm-dd hh:mm' and notice that $date is already set and not using the Date class. I understand I have to use strtotime which I have done to my $date $datestr = strtotime($date). I wish to subtract 5 minutes to the set time.
I've tried this simple thing $A = strtotime($date) - strtotime('-5 min'); But it does not aid me.
its simple to subtract the seconds of 5 minutes which is (5*60)=300 from your time
like this
$time = strtotime($date);
$time_new = $time-(5*60); // will time of the -5 min from the current $time
example
$date = date('d-M-Y g:i:s A'); // current date
echo $date."<br/>"; // output : 17-Feb-2014 8:35:58 AM
$time = strtotime($date); // convert current date to timestamp
$time_new = $time - (5*60); // subtract 5 minutes from current time.
$date_new = date('d-M-Y g:i:s A', $time_new); // convert time to the date again
echo $date_new; // output : 17-Feb-2014 8:30:58 AM

PHP- how to retrieve expire datetime

I need to retrieve the expire datetime starting from the current datetime + some time.
The example is:
$current_datetime = date('Y-m-d H:m:s');
$expire_time = 24;// 1 day in hours as int() for example ;)
$expire_datetime = date($current_datetime + $expire_time); //don't know how to do this in Y-m-d H:m:s format!!
I would like to write down an helper lib starting from this script.
Use this
$expire_datetime = date('Y-m-d H:m:s',strtotime("NEXT DAY"));
or
$expire_datetime = date('Y-m-d H:m:s',strtotime("tomorrow"));
$date = date('Y-m-d H:m:s');
$newdate = strtotime ( '+1 day' , strtotime ( $date ) ) ;
$newdate = date('y-m-d',$newdate)
echo $newdate;

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

Getting the difference between two time/dates using php?

I am wanting to find out the time difference in minutes between two dates which is in the format d-m-Y H:i (14-04-2009 12:15) using php?
Parse the times into timestamps using strtotime() and then simply subtract one from the other.
After that you can get the number of minutes, days and so on by using math functions.
For example:
// $date1 and $date2 are given
// the difference is in seconds
$difference = strtotime($date1) - strtotime($date2);
// getting the difference in minutes
$difference_in_minutes = $difference / 60;
Reference: strtotime()
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = date('m/d/Y H:i:s');
$model_current_time = date('Y-m-d H:i:s',
strtotime($currentDateTime));
echo $model_current_time."------";
$date = DateTime::createFromFormat('d/m/Y h:i:s A',
$row['model_creation_time']);//get from resouses
$new_date_format = $date->format('m/d/Y H:i:s');
$model_creation_time = date('Y-m-d H:i:s',
strtotime($new_date_format));
echo $model_creation_time;
$datetime1 = new DateTime($model_current_time);
$datetime2 = new DateTime($model_creation_time);
$interval = $datetime1->diff($datetime2);
echo $interval->d;
echo $interval->h;
echo $interval->s;

Categories