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
}
?>
Related
I want to check if a date/time string contains a day or not.
I used the date_parse() function but it automatically adds day => 1 if it doesn't find any day.
So I couldn't know if a string contains a day or not.
For example
$date = "2021-02";
How to know if this string contains a day or not?
The DateTime::createFromFormat method tests well that the format is adhered to. Entries like "2019-02-" or "2019-xx-23" are also recognized as incorrect.
$date = "2021-02-x";
$dateTime = DateTime::createFromFormat('Y-m-d',$date);
if($dateTime){
echo $date.' ok';
}
else {
echo 'wrong date '.$date;
}
I think you are expecting some thing like this.
<?php
$date1 = "2021-02";
$date2 = "2021-02-11";
$date3 = "2021-12";
$date4 = "2021-12-14";
$date1_array = explode("-", $date1);
$date2_array = explode("-", $date2);
$date3_array = explode("-", $date3);
$date4_array = explode("-", $date4);
if (count ($date1_array) == 3)
{
echo $date1 . ": It's a Date.";
echo "<br />";
}
if (count ($date2_array) == 3)
{
echo $date2 . ": It's a Date.";
echo "<br />";
}
if (count ($date3_array) == 3)
{
echo $date3 . ": It's a Date.";
echo "<br />";
}
if (count ($date4_array) == 3)
{
echo $date4 . ": It's a Date.";
echo "<br />";
}
?>
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)));
I'm trying to create an array of dates. The idea is that I add a number x days and the code, when adding the days skip only Sunday.
This is for laravel and I'm using carbon.
$date = Carbon::now();
$dates = [];
for($i = 1 ; $i < 20; $i++){
if($date->dayOfWeek === Carbon::SATURDAY){
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . " - Sunday <br> ";
} else {
echo $dates[$i] = $date->addDay(1)->format('d/m/Y') . "<br>";
}
When i use the constant SUNDAY to skip this date, its not working.
It goes on to consider Sunday as Monday
The problem is that you are checking if it's Saturday, and after that you're adding a day to it.
You need to echo the date before you add a day to it.
Try this:
if($date->dayOfWeek === Carbon::SUNDAY){ // checking if the current date is a sunday
echo $dates[$i] = $date->format('d/m/Y') . " - Sunday <br> "; // echo and add the current date to the array
$date->addDay(1);
} else {
echo $dates[$i] = $date->format('d/m/Y') . "<br>"; // echo and add the current date to the array
$date->addDay(1);
}
I got it with this code:
$inicialDate = Carbon::now();
$newDate = [];
for($i = 1; $i < 30; $i++)
{
$newDate[$i] = $inicialDate->addDay(1);
if($newDate[$i]->format('l') == "Sunday")
{
$newDate[$i] = $inicialDate->addDay(1);
}
echo $newDate[$i]->format('d/m/Y') . " - " . $newDate[$i]->format('l') . "<br>";
}
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
I want to check whether the current year is greater than a date string(D-M-Y) here is my code
$OldDate = "09-30-2011";
$OldYear = strtok($OldDate, '-');
$NewYear = date("Y");
if ($OldYear < $NewYear) {
echo "Year is less than current year"
} else {
echo "Year is greater than current year";
}
You can use strtotime():
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
UPDATE
Because you're using an unconventional datestamp, you have to use different methods, eg:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
NOTE: If you want to always be sure that your date gets correctly understood by strtotime, use YYYY-MM-DD
use the date function to get year
$OldDate = date("Y",strtotime("09-30-2011"));
$NewYear = date("Y",strtotime("now"));
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You can achieve your goal by doing:
$input_date = date("09-30-2011");
$input_date_arr = explode("-", $input_date);
$currYear = date("Y");
$inputYear = $input_date_arr[2];
if ($currYear > $inputYear) {
echo "Current year is greater than given year!";
} else {
echo "Current year is not greater than given year!";
}
$OldDate = "09-30-2011";
$OldYear = date('Y',strtotime($OldDate));
$NewYear = date("Y");
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You could convert the string to a timestamp, and check it with the current timestamp
if(time($OldDate) < time()){
// do stuff
} else {
// do other stuff
}
do this,
$dateString = '2021-02-24';
$yr = date("Y", strtotime($dateString));
$mon = date("m", strtotime($dateString));
$date = date("d", strtotime($dateString));