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
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';
}
I'm using DateTime to compare Timestamps. $firstDate below is 6 seconds more than $secondDate so I would like $interval to echo - 00:06. If it was the opposite then I would like + 00:06. No change should just display 00:00. Is there a way with DateTime to display the +/- change?
$firstdate = '00:00:06';
$seconddate = '00:00:12';
$one = new DateTime($firstdate);
$two = new DateTime($seconddate);
$interval = $two->diff($one);
if ($firstdate > $seconddate){
echo "-".$interval->format('%I:%S');
}
elseif($firstdate > $seconddate){
echo "+".$interval->format('%I:%S');
}
else{
echo $interval->format('%I:%S');
}
The current code displays 00:06 instead of +00:06 in this example, but if $firstdate and $seconddate are reversed it does echo -00:06 as intended
Just use if else:
if ($firstdate > $seconddate){
echo "-".$interval->format('%I:%S');
}
elseif($firstdate < $seconddate){
echo "+".$interval->format('%I:%S');
}
else{
echo $interval->format('%I:%S');
}
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.
What would be a good way to check if point is between start and extra.
point = 2010-06-20
start = 2010-06-17
extra = start + "1 week"
Any ideas would be appreciate it.
take a look at strtotime - an then simply compare the resulting timestamps:
$start = strtotime('2010-06-20');
$point = strtotime('2010-06-17');
$extra = strtotime('+1 week', $start);
if($start < $point && $extra > $point){
// it's bewtween...
}
Requires PHP 5.3
$period = new DatePeriod(
new DateTime('2010-06-17'),
DateInterval::createFromDateString('+1 day'),
new DateTime('2010-06-17 +1 week')
);
if (in_array(new DateTime('2010-06-20'), iterator_to_array($period))) {
// date is in range
}
Manual http://de2.php.net/manual/en/dateperiod.construct.php
I'd probably extend the DatePeriod class to have a contains methods:
class DateRange extends DatePeriod
{
public function contains(DateTime $dateTime)
{
return in_array($dateTime, iterator_to_array($this));
}
}
then you can do
$period = new DateRange(
new DateTime('2010-06-17'),
DateInterval::createFromDateString('+1 day'),
new DateTime('2010-06-17 +1 week')
);
if ($period->contains(new DateTime('2011-06-20'))) {
// date is in range
}
try this
$start_timestamp = strtotime('2010-05-17');
$end_timestamp = strtotime(date("Y-m-d", $start_timestamp) . " +1 week");
$point_timestamp = strtotime('2010-16-20');
if ($point_timestamp < $end_timestamp && $point_timestamp > $point_timestamp) {
// Do your work
}