I have this code that says if days between 2 dates,
but i need it to work with Y-m instead of Y-m-d
how to achieve this?
example: i need to check if 2016-10 is between 2016-09 and 2016-12
my code:
$paymentDate = date('Y-m-d', strtotime($date));
$contractDateBegin = date('Y-m-d', strtotime($dfrom));
$contractDateEnd = date('Y-m-d', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Extract Month and Year and compare it.
$paymentDateY = date('Y', strtotime($date));
$paymentDateM = date('n', strtotime($date));
$contractDateBeginY = date('Y', strtotime($dfrom));
$contractDateBeginM = date('n', strtotime($dfrom));
$contractDateEndY = date('Y', strtotime($dtom));
$contractDateEndM = date('n', strtotime($dtom));
if ($paymentDateY >= $contractDateBeginY && $paymentDateY <= $contractDateEndY
&& $paymentDateM >= $contractDateBeginM && $paymentDateM <= $contractDateEndM){
echo "is between";
}
else
{
echo "NO GO!";
}
use Y-m is oK: Demo
$paymentDate=date('Y-m', strtotime($date));;
$contractDateBegin = date('Y-m', strtotime($dfrom));
$contractDateEnd = date('Y-m', strtotime($dtom));
if (($paymentDate > $contractDateBegin) && ($paymentDate < $contractDateEnd)){
echo "is between";
}else{
echo "NO GO!";
}
Related
I have to check if the incoming date is between 3 and 6 months before today. If it is outside this range, it has to execute certain code.
below is the code
<?php
$date1 = '22-10-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))){
echo "Inside Range";
}else echo "Out of Range";
?>
For example if
Incoming date is 20-02-2018 - Out of Range.
Incoming date is 20-10-2017 - Inside Range.
Incoming date is 20-08-2017 - Out of Range.
You are checking with || in your case you need to use && because you need date BETWEEN
$date1 = '20-08-2017';
$date2 = date('d-m-Y' , strtotime('-3 months'));
$date3 = date('d-m-Y' , strtotime('-6 months'));
if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
echo "Inside Range";
}else {
echo "Out of Range";
}
Explanation:
Need to change your condition from if((strtotime($date1) < strtotime($date2)) || (strtotime($date1) > strtotime($date3))) to if((strtotime($date1) <= strtotime($date2)) && (strtotime($date1) >= strtotime($date3))){
It's also significantly easier if you're using DateTime objects:
$date1 = new DateTime('20-08-2017');
$date2 = new DateTime('-3 months');
$date3 = new DateTime('-6 months');
if($date1 < $date2 && $date1 > $date3) {
echo "Inside Range";
} else {
echo "Out of Range";
}
You can do like this:
$today=date_create(date("Y-m-d"));
$date=date_create("2018-06-12");
$diff=date_diff($today,$date)->format("%a");
if ($diff > 90 && $diff < 180) {
echo "Inside range";
}
else {
echo "Out of range";
}
I have many records in my database with different dates but my code doesn't catch the dates and it doesn't show up. What's the problem?
Here is my code
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s && $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s && $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
Try this in your if statement as I mentioned earlier in the comment below your question, problem is with your if statement. so change that to this:
if (strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e))
Try changing your if statement like this
$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s || $date2 <= $sem1_e){
echo $date2;
echo "First Sem";
}
else if($date2 >= $sem2_s || $date2 <= $sem2_e){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
I think you cannot use greater than or less than sign in a string
you should convert your variables as a strtotime.
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
Because you need to convert dates to UNIX timestamp for comparison.
The below will return the desired result,
if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
echo $date2;
echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
echo $date2;
echo "Second Sem";
}
else{
echo "error";
}
I am storing the date of a post in mysql database in this form y/m/d I want to check if the post is older than 3 days. Is there any way I can convert it to seconds ? and then check like so:
if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}
Using a mix of date() and strtotime() :
$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));
if ($myDate > $oldDate) {
echo "old post";
} else {
echo "new post";
}
Try, strtotime, that can parse various time string formats.
You can do following :
$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3, date('Y', $d1));
$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));
if ($d3 > $d2) {
echo 'old post';
}else {
echo 'new post';
}
I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>
If I have 2 dates, 21/05/2010 and 23/05/2010, how can I find out if 22/05/2006 7:16 AM exists in between them?
I am using the following code to calculate the min/max date and will then SELECT ALL records in the table that are clear to update them.
$today = date('l');
if($today == 'Wednesday'){
$min = date('d/m/Y', strtotime('0 days'));
$max = date('d/m/Y', strtotime('+6 days'));
}else if($today == 'Thursday'){
$min = date('d/m/Y', strtotime('-1 days'));
$max = date('d/m/Y', strtotime('+5 days'));
}else if($today == 'Friday'){
$min = date('d/m/Y', strtotime('-2 days'));
$max = date('d/m/Y', strtotime('+4 days'));
}else if($today == 'Saturday'){
$min = date('d/m/Y', strtotime('-3 days'));
$max = date('d/m/Y', strtotime('+3 days'));
}else if($today == 'Sunday'){
$min = date('d/m/Y', strtotime('-4 days'));
$max = date('d/m/Y', strtotime('+2 days'));
}else if($today == 'Monday'){
$min = date('d/m/Y', strtotime('-5 days'));
$max = date('d/m/Y', strtotime('+1 days'));
}else if($today == 'Tuesday'){
$min = date('d/m/Y', strtotime('-6 days'));
$max = date('d/m/Y', strtotime('0 days'));
}
DateTime::diff
Create a DateTime::diff between 21/05/2010 and 22/05/2006 7:16 AM, as well as a DateTime::diff between 23/05/2010 and 22/05/2006 7:16 AM.
Then check that the first DateTime::diff is > 0, and the second is < 0
Update : used Datetime::createFromFormat, which is a php5.3 method
Update2 : Tested code sample. Produces expected output.
<?php
$datetime_lower = DateTime::createFromFormat('d/m/Y', '21/05/2010');
$datetime_upper = DateTime::createFromFormat('d/m/Y', '23/05/2010');
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', '22/05/2006 7:16 AM');
var_dump($datetime_lower < $datetime_compare);
var_dump($datetime_upper > $datetime_compare);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
echo " + date is between";
} else {
echo " date is not between";
}
Also, there is a procedural date_diff function
Use strtotime
$date1 = strtotime($date1);
$date2 = strtotime($date2);
$datefind = strtotime($datefind);
if ($datefind >= $date1 && $datefind <= $date2)
Explode your dates and use the parts in a mktime() to get their timestamp values. Then it's a simple matter of checking if your timestamp is larger or smaller than the others.
http://php.net/manual/en/function.mktime.php
Pseudocode:
$parts= explode("/", "21/05/2010");
$timestamp1= mktime($parts[0], $parts[1], ...);
if($timestamp1 < $timestamp2...) {
print "timestamp1 is older then timestamp2";
}