Compare time to separate days - php

I have some DB entries, they have timestamps. And all I want is to draw a separate line between days. And also, I need the day to start not at 00:00, but at 07:00. It's like an offset for the day start.
Now I have that (for context):
foreach($logs as $log) {
$cur_date = $log[0]['timestamp'];
echo "<p>".$log[0]['content']."</p>";
}
Is there a simple workaround for the problem I've described?
Thank you!

$prev_date = 0;
foreach($logs as $log) {
$cur_date = strtotime($log[0]['timestamp']);
$cur_day_beginning = strtotime(date("Y-m-d 07:00:00", $cur_date));
if ($cur_date >= $cur_day_beginning && $prev_date < $cur_day_beginning) {
echo "<hr/>";
}
$prev_date = $cur_date;
echo "<p>".$log[0]['content']."</p>";
}

Related

Avoid loop for Date add by day?

I am using Carbon to add number of days, it there a way to avoid using for and/or while loop?
Add the numbers of days ($skipDayBy) and add the number of days if found in $excludeDatesPublic or $excludeDatesManual?
For example working demo:
function calculateDate($skipDayBy = 0) {
$excludeDatesPublic = ['2019-08-28'];
$excludeDatesManual = ['2019-09-01'];
$date = Carbon::now();
for($i = 0; $i < $skipDayBy; $i++) {
$date = $date->addDays(1);
while(in_array($date->toDateString(), $excludeDatesPublic) || in_array($date->toDateString(), $excludeDatesManual))
{
$date = $date->addDays(1);
}
}
return $date->toDateString();
}
echo calculateDate(4);
Returned 2019-09-02 as expected if today date is 2019-08-27.
Maybe you're looking for https://github.com/kylekatarnls/business-day that allows you to add days skipping holidays.
Alternatively, you can use the periods:
$skipDayBy = 5;
$excludeDatesPublic = ['2019-09-01'];
$excludeDatesManual = ['2019-09-04'];
$exclude = array_merge($excludeDatesPublic, $excludeDatesManual);
$date = CarbonPeriod::create(Carbon::now(), $skipDayBy)
->addFilter(function (Carbon $date) use ($exclude) {
return !in_array($date->format('Y-m-d'), $exclude);
})
->calculateEnd();
var_dump($date); // 2019-09-06 18:50:17 if run at 2019-08-31 18:50:17
With PHP shortly
$day='2019-12-12';
$date = date('Y-m-d', strtotime($day . " +4 days"));
echo $date;
Output
2019-12-16
Or u can use
$date= date('Y-m-d', strtotime('+4 days', strtotime($day)));

Laravel Carbon get next occurrence of particular date from current date

Using Carbon with laravel 5.6.
I want write a code that give me next occurrence of date from current date.
E.g Give next 31st May date
Scenario 1 :
Input : $currentDate = '01-30-2019'; // MM-DD-YYYY format
Expected Output: $next31May = '05-31-2019';
Scenario 2 :
Input : $currentDate = '07-04-2019'; // MM-DD-YYYY format
Expected Output: $next31May = '05-31-2020';
Update:
I tried below code but not satisfy
<?php
public function nextOccurance()
{
$now = Carbon::now();
$month= $now->month;
$year = $now->year;
if($month > 6)
{
echo Carbon::createMidnightDate($year+1, 5, 31);
}
else
{
echo Carbon::createMidnightDate(null, 5, 31);
}
exit();
}
?>
Thank You in advance.
public function nextOccurance()
{
// the 31th of May of the current year
$day = Carbon::createFromFormat('m-d', '05-31');
$now = Carbon::now();
// If today after $day
if($now >= $day) {
// Gat a next year
$day->modify('next year');
}
echo $day->format('Y-m-d');
exit();
}
this is like to get the next birthday.
class Test
{
public static function getNextBirthday($date)
{
// set birthday from current year
$date = Carbon::createFromFormat('m-d-Y', $date);
$date->year(Carbon::now()->year);
// diff from 31 may to now
// its negative than add one year, otherwise use the current
if (Carbon::now()->diffInDays($date, false) >= 0) {
return $date->format('m-d-Y');
}
return $date->addYear()->format('m-d-Y');
}
}
echo Test::getNextBirtday('05-31-1990');
I wish this will help you to fix informed issue.
$event = Carbon::parse('31 May');
if (Carbon::now() >= $event){
$nextEvent = $event->addYear();
} else {
$nextEvent = $event;
}
echo $nextEvent->format('m-d-Y');
Carbon provides a nice and a fluent interface to this kind of stuff.
You can lastOfMonth() to get the last day of month. for adding year you can add addYear(1)
$now = Carbon::now();
$month= $now->month;
$year = $now->year;
if($month > 6)
{
echo $now->addMonth(5)->lastOfMonth();
}
else
{
echo $now->addYear(1);
}
exit();
}

create calendar with seven following day

I want to draw a perpetual calendar on 7 following day
$dt = new DateTime;
if (isset($_POST['annee']) && isset($_POST['semaine'])) {
$dt->setISODate($_POST['annee'], $_POST['semaine']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
};
$annee= $dt->format('o');
$semaine= $dt->format('W');
$KlendrierPerpetuel="";
$KlendrierPerpetuel.='<table><tr>';
do {
$KlendrierPerpetuel.='<td>'.$dt->format('D').'</td>';
$dt->modify('+1 day');
} while ($semaine == $dt->format('W'));
$KlendrierPerpetuel.='</tr></table>';
echo $KlendrierPerpetuel;
it creates a table width day of the week instead of 7 next day
Well... it's actually a lot more simple...
$format = "y/m/d";
$date_arr = [];
for($i = 0; $i < 6; $i ++){
if($i = 0)
$date_arr[] = date($format);
else
$date_arr[] = date($format, strtotime("+$i day");
}
strtotime can parse date from a string like "+1 day" or "+1 week", so have the next day is quite simple. So you just need to loop the process and it's done.
PS : In your initial post, you seem to get some post parameters, you just need to put them in a strtotime() in the if.
What you're looking for isn't completely clear, but I took my best guess and added user controls to implement it. You should be able to modify this to suit your needs:
<html>
<form method='post'>
<input type='numeric' name='year' value='<?= $_POST['year'] ?: date('Y') ?>'>
<input type='numeric' name='week' value='<?= $_POST['week'] ?: date('W') ?>'>
<input type='submit'>
</form>
<?php
$dt = new DateTime;
if (isset($_POST['year']) && isset($_POST['week'])) {
$dt->setISODate($_POST['year'], $_POST['week']);
} else {
$dt->setISODate($dt->format('o'), $dt->format('W'));
};
$year= $dt->format('o');
$week= $dt->format('W');
$KlendrierPerpetuel="";
$KlendrierPerpetuel.='<table><tr><td>'.$dt->format('M').'</td>';
do {
$KlendrierPerpetuel.='<td>'.$dt->format('d').'<br>'.$dt->format('D').'</td>';
$dt->modify('+1 day');
} while ($week == $dt->format('W'));
$KlendrierPerpetuel.='<td>'.$dt->format('M').'</td></tr></table>';
echo $KlendrierPerpetuel;
This allows user specification of the year and week number, and displays the days of that week.

PHP detect the first loop of the new day

My solutions works mostly but not really always :) What is the reliable/best solution?
<?php
date_default_timezone_set('UTC');
$seconds_today = time() - strtotime('today');
while (true) {
if ($seconds_today > time() - strtotime('today')) {
print('First loop of the new day');
}
$seconds_today = time() - strtotime('today');
sleep(1);
}
?>
There's no need to bother with seconds/time, if all you're interested is the day. In addition, if you check the time twice per loop, there's a chance you miss the switch, so it's more robust to check it exactly once.
<?php
date_default_timezone_set('UTC');
$lastDay = date("Y-m-d");
while (true) {
$now = date("Y-m-d");
if ($now !== $lastDay) {
$lastDay = $now;
print("First loop of the new day");
}
sleep(1);
}
?>
Having said that, I'd use something like cron, if possible..
I am not sure of PHP syntaxe, but here is the idea :
<?php
date_default_timezone_set('UTC');
$previous_day = 0;
while (true) {
if ($previous_day != strtotime('today')) {
print('First loop of the new day');
$previous_day = strtotime('today');
}
sleep(1);
}
?>

php check if date has passed

I'm getting dates from a Wordpress field and I need to check if the dates have past or still to come.
$dates = ['date'=>'02/12/13','date'=>'10/12/14','date'=>'14/01/15'];
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if($the_date < date('d/m/y')){
echo 'gone';
}else{
echo 'to come';
}
}
The foreach echos out this.
02/12/13 22/11/14 gone
10/12/14 22/11/14 gone
14/01/15 22/11/14 gone
27/01/15 22/11/14 to come
10/02/15 22/11/14 gone
It looks like it's just checking the first day date.
A better option is to use the DateTime class. It allows to compare two DateTime instances using comparison operators.
$dates = ['02/12/13','10/12/14','14/01/15'];
foreach ($dates as $date) {
$the_date = \DateTime::createFromFormat('d/m/y', $date);
$now = new \DateTime();
echo $date." ".($the_date < $now ? 'gone' : 'to come')."\n";
}
The problem you see is because the dates are being compared as strings. The current date is "22/11/14" so it will be greater than any other date with a day starting with "1" or "0".
PD: Your array contains many elements using the same 'date' key. That is a problem so I've removed them in my example.
<?php
$dates = array('02/12/13','10/12/14','14/01/15');
$now = mktime(0,0,0);
foreach($dates as $date) {
$tmp = explode('/',$date);
$date_time = mktime(0,0,0,intval($tmp[1]),intval($tmp[0]),intval($tmp[2]));
echo $date . ' ' . ($now > $date_time?'gone':'to come') . "\n";
}
Use PHP's DateTime API :
$date='02/12/13';
if(\DateTime::createFromFormat('d/m/y',$date) < new \DateTime()){
//date is in the past
}else{
//date is either today or in the future
}
Offical PHP doc:
http://php.net/manual/en/class.datetime.php
Best way is to use timestamp: Try this:
foreach ($dates as $date){
$the_date = $date['date'];
echo $the_date;
echo " ";
echo date('d/m/y');
echo " ";
if( strtotime($the_date) < time() )
{
echo ' is gone';
}
else
{
echo ' is to come';
}
}
Keep her simple:
// $date is the date you need to compare to today
$date = ("2015 10 03");
// Make sure their formats are purely numeric and match
if ($date->format('m.d.y') >= date('m.d.y'))
{
your procedure...
}
I suggest using the capabilities of the DateTime class instead. Then you can do the check as follows:
<?php
$then = $reset_date;
$then = new DateTime($then);
$now = new DateTime(date("m-d-Y"));
$sinceThen = $then->diff($now);
$new = new DateTime($reset_date);
$old = new DateTime(date("m-d-Y"));
if ( $old->modify('+1 year') < $new) {
echo "<font color='red'>Reset now <br></font>";
echo "<font color='orange'>$sinceThen->y years <br></font>";
echo "<font color='orange'>$sinceThen->m months </font>";
echo "<font color='orange'>$sinceThen->d days have passed.<br></font>";
} else {
echo "<font color='green'> $sinceThen->y years <br>
$sinceThen->m months $sinceThen->d days till to Reset.</font>";
//Combined
}
?>

Categories