Echoing time from now and one hour later - php

Here I have set the my Centos time Zone.> sudo hwclock – show
Tue 04 Feb 2014 10:23:10 AM AFT -0.596389 seconds
asia/kabul
Now on PHP I want to echo from 10:23:10 AM to 11:23:10 AM
Here I have set my code for the echoing.
<? echo (date('G', time())+5) ;
echo(':00 To ');
echo (date('G', time())+6);
echo(':00'); ?>
Now as result of my above echoing PHP code I get the result of
15:00 To 16:00
But instead I want to get the echo of below or as what ever my HTTP server time is from NOW to 1 hour next.
10:23:10 AM to 11:23:10 AM

You should try strtotime(). Something like this:
echo date('h:i:s A'), ' to ', date('h:i:s A', strtotime('+1 hour'));

echo date('Y-m-d H:i:s', strtotime('now')), ' to ', date('Y-m-d H:i:s', strtotime('+1 hour'));
For date format, please read: http://www.php.net/manual/en/function.date.php
For your format:
echo date('h:i:s A', strtotime('now')), ' to ', date('h:i:s A', strtotime('+1 hour'));

Add to the time and then print the date.
echo date('G',time()+3600);
time() returns number of seconds since the unix epoch, then you add 3600 seconds for 1 hour and you use that in your date.

You can use explode function.
<?PHP
$time = date('G:i:s');
$eTime = explode(':', $time);
$timePlusOneHour = $eTime+1 . ":{$eTime[1]}:{$eTime[2]}";
echo $time . " To " . $timePlusOneHour;

Related

Converting Timezones from UTC

I'm having trouble converting timezone's from UTC to a user selected timezone. The problem seems to be Daylight Savings Time.
Here is an example I just coded.
<?php
date_default_timezone_set("UTC");
$timezone = -5.0;
$timestamp = time();
$local_time = $timezone * 3600 + $timestamp;
echo date( "m/d/Y - h:i A", $local_time );
?>
When I run the test file it returns 07/21/2014 - 04:29 PM. The current time is actually 5:29. The problem is Daylight Savings Time, where our clocks are turned back an hour.
How can I remedy this problem, or is there a more effective method for adjusting timestamps?
Easiest solution I see is making users select whether DST is currently in effect where they live, as not every country/timezone uses DST. If it is in effect then simply modifying the $timezone variable to +1 would suffice, but would require each user to manage whether DST is in effect or not.
Thank-you in advance.
EDIT:
I tried using DateTime but it was still off...
<?php
$timestamp = time();
echo 'Unix timestamp: ' . $timestamp . "<br/>";
echo 'Unix date: ' . date( "m/d/Y - h:i A", $timestamp ) . "<br/><br/>";
$dt = date_create_from_format('U', $timestamp);
date_timezone_set($dt, new DateTimeZone('America/New_York'));
$adjusted_timestamp = date_format($dt, 'U') + date_offset_get($dt);
echo 'America/New_York: ' . $adjusted_timestamp . "<br/>";
echo 'America/New_York: ' . date( "m/d/Y - h:i A", $adjusted_timestamp );
?>
The results were off by +2 hours. Returns 7:47 PM, it is currently 5:47 PM.
Unix timestamp: 1405979278
Unix date: 07/21/2014 - 11:47 PM
America/New_York: 1405964878
America/New_York: 07/21/2014 - 07:47 PM

PHP get next occurrence of Monday from a certain date (with time)

I'm looking for the next Thursday after a specific date, say 2014-02-25. The problem I'm having here is that when I use the below code, the time seems to be erased.
<?php
$timestamp = '2014-02-25 10:30:00';
echo date('Y-m-d H:i:s', strtotime("next Thursday", strtotime($timestamp)));
?>
The result I am getting is 2014-02-27 00:00:00 when I would like it to be 2014-02-27 10:30:00
Is there something I am missing here that is causing the time to be set to midnight?
I appreciate the help, thanks.
There is no time format that can directly express this. You need to produce a format like
next Thursday 10:30:00
... manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
The same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
The documentation for so called relative time formats can be found here

how to convert hour minute and pm/m to strtotime

I have a time value 04:30 (h:mts am/pm) pm. I want to convert this to strtotime
Also I need to convert back from strtotime to this format (h:mits am/pm)
Thanks
Try this:
$time_string = '04:30 pm';
$time = strtotime($time_string);
echo 'PHP Time: ' . $time . PHP_EOL;
echo 'Human Time: ' . date('h:i a', $time);
Demo here: http://ideone.com/SR58g
read this documentation of php http://php.net/manual/en/function.time.php

subtract 6 hours from date('g:i a', strtotime($time_date_data));

I am using the following code to transform a universal time code into something a little more user friendly.
$meeting_time = date('g:i a', strtotime($time_date_data));
But now I need to subtract 6 hours from meeting_time. Should I do it after the code above or can I work it into the same date function?
Something like:
$meeting_time = date('g:i a' - 6, strtotime($time_date_data));
$meeting_time = date('g:i a', strtotime($time_date_data) - 60 * 60 * 6);
String-to-time (strtotime) returns a Unix Time Stamp which is in seconds (since Epoch), so you can simply subtract the 21600 seconds, before converting it back to the specified date format.
Another approach:
$meeting_time = date('g:i a', strtotime('-6 hours', strtotime($time_date_data)));
Try this:
// 6 hours, 3600 seconds in an hour
$meeting_time = date('g:i a', strtotime($time_date_data) - 6 * 3600);
The way of OO PHP:
$date = new DateTime(); // current time
echo 'Current: '. $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT3H55M10S'));
echo $date->format('Y-m-d H:i:s') . "\n";
For subtract 6 hrs:
$date = new DateTime('2017-01-20'); // pass $time_date_data to here
$date->sub(new DateInterval('PT6H'));
You should be able to do this:
$meeting_time = date('g:i a', strtotime($time_date_data));
date_add($meeting_time, - date_interval_create_from_date_string('6 hours'));

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

Categories