I have a timestamp in '2013-04-10T09:00:00Z' format and based on this I want to check the requested datetime format is within 10 minutes or not.
Try this:
$now = time();
$timestamp = strtotime('2013/04/08T09:00:00Z');
$timediff = $now - $timestamp;
if (floor($timediff/60) > 10) {
echo 'Time is 10 minutes older';
}
else {
echo 'Time is not older then 10 min';
}
Below is the code that I tried and it worked for me,
$currentDateTime =new DateTime();
$currentDateTime->setTimezone(new DateTimeZone('UTC'));
$userDateTime = new DateTime($timestamp); //convert users incoming
$diff = $now->diff($then);
$minutes = ($diff->format('%a')*1440)+($diff->format('%h')*60)+ ($diff->format('%i'));
if($minutes <=10)
{
//your code goes here
}
else
{
//time difference is more then 10 minutes<br/>
}
Thanks Francisco for all your post.
Related
in my Controller
$ldate = date('Y-m-d ');
$b = DB::table('warehouse_products_sell')
->select([
'warehouse_products_sell.dueto_date'
])
->get();
my output
$b->dueto_date = 2017-10-26
$ldate = 2017-10-07
i want to check
if ($b->dueto_date - $ldate < 5 ){
alert('hi')
}
NOTE : 5 ( day )
not sure how to do .
Laravel uses Carbon library , so you may easily rely on it's cool API,
here is a quick example :
use Carbon\Carbon;
$dt1 = Carbon::createFromFormat('Y-m-d', $b->dueto_date);
$dt2 = Carbon::createFromFormat('Y-m-d', $ldate);
$diff = $dt1->diffInDays($dt2);
if ($diff < 5) {
echo "hi";
}
$DueDate = new DateTime($b->dueto_date); // Due Date
$ldate = new DateTime('Y-m-d') // Your date
$interval = $DueDate->diff($ldate);
if($interval->d < 5){
echo '<script>alert("less than 5 days remaining")</script>';
}
Do it like below:
$days = floor((strtotime($b->dueto_date)- strtotime($your_date))/ (60 * 60 * 24));
if ($days < 5 ){
echo "less than 5 days remaining"; //it's php not jQuery
}else{
echo "$days days remaining";
}
Sample example: https://eval.in/875753
i'm trying to find and calculate between startime, finish time as: starttime + 1 hour and current time. if current time is between start and finish i must be print message such as please try after 1 hour:
$current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
$user_current_time = $current_date_time->format("H:i:s");
$start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
$finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));
$date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
$date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
$date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
if ($date1 > $date2 && $date1 < $date3)
{
echo 'here';
}
this code is not correct and i can not fix that,
You can try this, it shows the difference in minutes:
$current_date_time = new DateTime("now", new DateTimeZone("Asia/Tehran"));
$user_current_time = $current_date_time->format("H:i:s");
$start_limit_time = date("H:i:s",strtotime('2015-09-15 14:57:31'));
$finish_limit_time = date('H:i:s', strtotime($start_limit_time) + (60 * 60 * 1));
$date1 = DateTime::createFromFormat('H:i:s', $user_current_time);
$date2 = DateTime::createFromFormat('H:i:s', $start_limit_time);
$date3 = DateTime::createFromFormat('H:i:s', $finish_limit_time);
if ($date1 > $date2 && $date1 < $date3)
{
$tryAgainIn = $date3->diff( $date1 );
// just minutes
echo "try again in ".$tryAgainIn->format( "%i minutes" );
// or hours and minutes
$hours = $tryAgainIn->format('%h');
$minutes = $tryAgainIn->format('%i');
echo "try again in $hours hours and $minutes minutes";
}
For more information take a look at: DateTime::diff
At first you should avoid operating with strings format, as they should only be used IMHO to printing and retrieving data from outside. Use only timestamp or OOP methods.
I believe, that this is something you are looking for:
$startTime = new DateTime('2015-09-15 14:57:31');
$endTime = clone $startTime;
$endTime->modify('+1 hour');
if ($startTime->getTimestamp() <= time() && time() < $endTime->getTimestamp()) {
echo 'here';
}
I wonder why you need to use H:i:s format. Can you give some bigger picture?
Edit: Try this, as prior to now I did not fully understand what you want to do ;)
$origin = new DateTime('2015-09-15 14:57:31');
$startTime = new DateTime('today '.$origin->format('H:i:s'));
$endTime = clone $startTime;
$endTime->modify('+1 hour');
if ($startTime->getTimestamp() <= time() && time() < $endTime->getTimestamp()) {
echo 'here';
}
So I have a piece of code on a certain page of my site that does things with timestamps. Pretty much what it does is there is a UNIX timestamp that is placed in the database from each individual Purchase Order. Once a certain amount of time has passed and nothing has been done to that Purchase Order then an indication will begin flashing on the page with the amount of hours that is past due. Once someone takes action then the flashing indication goes away.
Now, everything is working perfectly fine. The issue I am having is that the indicator should only take Monday thru Friday into account. Not the weekends. Also, I've set the hours from 9am to 5pm est but the code seems to 100% skip all these restrictions and just takes all days and times into consideration.
I've placed the code below and as you can see I've set the restrictions of days and time but it seems to be voided somehow. Any help would be much appreciated with this issue.
$current_stardate = time();
$past_stardate = $stardate['time_stamp'];
$placer = ($current_stardate - $past_stardate) / 3600;
$from = date("Y-m-d H:i:s", $current_stardate);
$to = date("Y-m-d H:i:s", $past_stardate);
define('DAY_WORK', 28800); // 9 * 60 * 60
define('HOUR_START_DAY', '09:00:00');
define('HOUR_END_DAY', '17:00:00');
$date_begin = $to;
$date_end = $from;
$d1 = new DateTime($date_begin);
$d2 = new DateTime($date_end);
$period_start = new DateTime($d1->format('Y-m-d 00:00:00'));
$period_end = new DateTime($d2->format('Y-m-d 23:59:59'));
$interval = new DateInterval('P1D');
$period = new DatePeriod($period_start, $interval, $period_end);
$worked_time = 0;
$nb = 0;
foreach($period as $date){
$week_day = $date->format('w'); // 0 (for Sunday) through 6 (for Saturday)
if (!in_array($week_day,array(1, 5)))
{
if ($date->format('Y-m-d') == $d1->format('Y-m-d'))
{
$end_of_day_format = $date->format('Y-m-d '.HOUR_END_DAY);
$d1_format = $d1->format('Y-m-d H:i:s');
$end_of_day = new DateTime($end_of_day_format);
$diff = $end_of_day->diff($d1)->format("%H:%I:%S");
$diff = split(':', $diff);
$diff = $diff[0]*3600 + $diff[1]*60 + $diff[0];
$worked_time += $diff;
}
else if ($date->format('Y-m-d') == $d2->format('Y-m-d'))
{
$start_of_day = new DateTime($date->format('Y-m-d '.HOUR_START_DAY));
$d2_format = $d2->format('Y-m-d H:i:s');
$end_of_day = new DateTime($end_of_day_format);
$diff = $start_of_day->diff($d2)->format('%H:%I:%S');
$diff = split(':', $diff);
$diff = $diff[0]*3600 + $diff[1]*60 + $diff[0];
$worked_time += $diff;
}
else
{
$worked_time += DAY_WORK;
}
}
if ($nb> 10)
die("die ".$nb);
}
$the_work = $worked_time/60/60;
$genesis_stardate = strtotime($stardate['date_purchased']);
if($past_stardate == NULL)
{
$the_work = NULL;
$future_days = NULL;
}
else
{
$future_days = ($current_stardate - $past_stardate) / 3600;
}
$date is not defined. Try to define it, and the problem should be solved.
I want to let the user renew a post after 10 days since posted and have a count down of how many days left before they can click renew.
My attempt failed, could someone help me with this please?
$date = new DateTime($date);
$finish = new DateTime();
$difference = $finish - $date;
if ($date->diff($finish)->days > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}
try this:
<?php
$date = new DateTime($date);
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}
?>
is it possible to do something like this.
i was trying to do it but couldn't...
this is what i was trying to do
$date2second = strtotime('2013-03-5');
$date1week = strtotime('2013-03-5') + 604800;
//passed less than
//$datetillnextweek = strtotime('2013-03-5') + 1209600;
$datetillnextweek = strtotime(date('Y-m-d')) + 1209600;
echo "$date2second <br>";
echo "$date1week <br>";
echo "$datetillnextweek <br>";
if($date2second < $date1week && $date2second <= $datetillnextweek)
{
echo "action";
}
$now = new DateTime('2013-03-28');
$one_week = new DateTime();
$one_week->modify('+1 week');
$two_weeks = new DateTime();
$two_weeks->modify('+2 weeks');
if ($now > $one_week && $now < $two_weeks)
{
// you're here
}
See it in action
check the date add function and other date class options. you can specify the interval and the format to present it .
http://www.php.net/manual/en/datetime.add.php
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7D'));
echo $date->format('Y-m-d') . "\n";
?>
To see if its in a time period you can also use http://www.php.net/manual/en/datetime.diff.php to see if the difference iis larger then 7 days