Collect data from last 5 days in MySQL - php

I have following code
$date = date("Y-m-d");
public function ip2_exists() {
global $db,$date;
$code = $db->select("imp","id",array("date"=>$date,"ip"=>$this->ip()));
if($code->num_rows<1){
return false;
}
else {
return true;
}
}
It collects the current day data and it works fine. But I want to collect the last 5 days date.
How to do that in php?

Try:
$date = date("Y-m-d");
public function ip2_exists()
{
global $db,$date;
// Used to keep count track
$countDays = 0;
// Default return of true
$checkedData = true;
while($countDays < 5)
{
$code = $db->select("imp","id",array("date"=>$date,"ip"=>$this->ip()));
if($code->num_rows < 1)
{
$checkedData = false;
}
$countDays++;
$date = date("Y-m-d", strtotime( $date." -1 day"));
}
return $checkedData;
}

Related

PHP detect if between specified date and now() matched specified condition

I want to know how to detect if a date range matched specified condition:
Expected results:
<?php
$start_date1 = '2016-05-06 00:00:00';
$start_date2 = '2016-01-06 00:00:00';
$result1 = is_date_range_exceeds_3_months($start_date1);
$result2 = is_date_range_exceeds_3_months($start_date2);
//lets say 'now' is '2016-06-06 00:00:00'
//Expected result of $result1 = false
//Expected result of $result2 = true
?>
(Please make correction to my question as I think this question is not in correct format/words)
Thanks!
You could use DateTime for this.
function is_date_range_exceeds_3_months($strDate)
{
$userDate = new \DateTime($strDate); // #todo: Check if is valid
$checkDate = new \DateTime(); // By default date seed is now
$checkDate->modify('+3 months'); // Set period
if($userDate > $checkDate) {
return true;
} else {
return false;
}
}
This is just a tip, sorry if it contains some typos.
Edit by OP:
function is_date_range_exceeds_x_months($month_limiter, $start_date) {
$userDate = new DateTime($start_date); // #todo: Check if is valid
$userDate = $userDate->format('Y-m-d H:i:s');
$checkDate = new DateTime(); // By default date seed is now
$checkDate->modify('-' . $month_limiter . ' months'); // Set period
$checkDate = $checkDate->format('Y-m-d H:i:s');
if ($userDate < $checkDate) {
return true;
} else {
return false;
}
}
<?php
$start_date1 = '2016-05-06 00:00:00';
$start_date2 = '2016-01-06 00:00:00';
$result1 = is_date_range_exceeds_3_months($start_date1);
$result2 = is_date_range_exceeds_3_months($start_date2);
//lets say 'now' is '2016-06-06 00:00:00'
function is_date_range_exceeds_3_months($start_date)
{
$lastdates=date('Y-m-d H:i:s', strtotime('-3 month'));
$current_dates=date("Y-m-d H:i:s");
echo 'currentdate'.$current_dates.'<br/>';
echo 'Lasytdate'.$lastdates.'<br/>';
if (($start_date > $lastdates) && ($start_date < $current_dates))
{
return true;
}
else
{
return false;
}
}
//Expected result of $result1 = false
//Expected result of $result2 = true
?>

Current time plus one for date validation?

I'm having issues with a booking system that i'm trying to customize. It seems that the validation doesn't work on the current date and time if i select the current day.
The validation is a as following.
if (strtotime($str) < time()) {
But that doesn't allow me to book on the current date, even if the time is over current time, not sure if i should add + one to the validation or what. Any ideas would be very helpful.
Here is the full function.
public function _validate_date($str) {
if (strtotime($str) < time()) {
$this->form_validation->set_message('_validate_date', 'Date must be after today, you can only make future reservations!');
return FALSE;
} else {
return TRUE;
}
}
Try the following solution:
$datetime1 = new DateTime($str);
$datetime1->setTime(0, 0, 0);
$datetime2 = new DateTime();
$datetime2->setTime(0, 0, 0);
//get the diff.
$diff = $datetime2->diff($datetime1);
$days = (int) $diff->format("%R%a");
if ($days < 0) {
echo 'past days';
} elseif ($days == 0) {
echo 'today';
} else {
echo 'future days';
}
A working example you can find here: https://3v4l.org/QQ4Pu
Your function with the new solution:
public function _validate_date($str) {
$datetime1 = new DateTime($str);
$datetime1->setTime(0, 0, 0);
$datetime2 = new DateTime();
$datetime2->setTime(0, 0, 0);
//get the diff.
$diff = $datetime2->diff($datetime1);
$days = (int) $diff->format("%R%a");
if ($days <= 0) {
$this->form_validation->set_message('_validate_date', 'Date must be after today, you can only make future reservations!');
return false;
} else {
return true;
}
}
And a working example of your function: https://3v4l.org/HPTVF
If you want check, that current time is not higher current date, try this:
if (strtotime($str) < strtotime(date('Y-m-d 23:59:59')) {

check upcomming birthday in a week 2 week or in a month using php

how to check is birthday is in this week,2week,or in month i have used below code to check but it return wrong calculation.
public function CountDown($birthdate, $days=7)
{
list($y,$d,$m) = explode('/',$birthdate);
$today = time();
$event = mktime(0,0,0,$m,$d,$y);
$apart = $event - $today;
if ($apart >= -86400)
{
$myevent = $event;
}
else
{
$myevent = mktime(09,0,0,$m,$d,$y);
}
$countdown = round(($myevent - $today)/86400);
if ($countdown <= $days)
{
return true;
}
return false;
}
Try this:
function CountDown($birthdate, $days=7)
{
# create today DateTime object
$td = new DateTime('today');
# create birth DateTime object, from format Y/d/m
$bd = DateTime::createFromFormat('!Y/d/m', $birthdate);
# set current year to birthdate
$bd->setDate($td->format('Y'), $bd->format('m'), $bd->format('d'));
# if birthdate is still in the past, set it to new year
if ($td > $bd) $bd->modify('+1 year');
# calculate difference in days
$countdown = $bd->diff($td)->days;
# return true if day difference is within your range
return $countdown <= $days;
}
demo
This worked for me
class Birthday{
public function CountDown($birthdate, $days=7)
{
list($y,$d,$m) = explode('/',$birthdate);
$today = time();
$event = mktime(0,0,0,$m,$d,$y);
$apart = $event - $today;
if ($apart >= -86400)
{
$myevent = $event;
}
else
{
$myevent = mktime(09,0,0,$m,$d);
}
$countdown = round(($myevent - $today)/86400);
if (($countdown <= $days))
{
return true;
}
return false;
}
}
$bday = new Birthday;
$count = $bday->CountDown("1969/16/11"); //today is 2014/14/11
var_dump($count); //returns true.
I just removed the year from the mktime() in $myevent. This changed the answers to be accurate.
The other way that it was being done made $countdown to be a huge negative number.

List all dates and check passed date

Example (Today is 13.05.2013):
11/05/2013,20/05/2013,9/05/2013 <-- false;
10/03/2013,14/04/2013,12/05/2013 <-- true;
15/06/2013,11/06/2013,8/06/2013 <-- false;
13/05/2013,10/04/2013,02/05/2013 <-- false (is today = false)
PHP :
function outdate($dates) {
$dates = str_replace('/','-',$dates);
$dates = explode(',',$dates);
$today = time();
foreach ($dates as $date) {
if($today > strtotime($date)) {
// ????
}
}
}
I have done with some parts. Please help me to do return true or false
I will use my function like..
if( outdate('11/05/2013, 12/05/2013, 9/05/2013') ) {
// do something
}
Anyone can help me ?
Suppose this is what you need:
function outdate($dates) {
$res = true;
$dates = str_replace('/','-',$dates);
$dates = explode(',',$dates);
$today = time();
foreach ($dates as $date) {
if($today < strtotime($date)) {
$res = false;
break;
}
}
return $res;
}
It will return true if all passed dates are in past. And false if at least one date is today or in future.
function outdate($dates) {
$flag = false;
$dates = str_replace('/','-',$dates);
$dates = explode(',',$dates);
$today = time();
foreach ($dates as $date) {
if($today > strtotime($date)) {
$flag = true;
break;
}
}
return $flag;
}
You can use an array instead, something like this (untested)
$array = array("11/05/2013", "12/05/2013", "9/05/2013");
$result = checkDates($array);
function checkDates($array)
{
foreach($array as $a)
{
$date = strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $a));
if($date>time()) return false;
}
return true;
}
I found my another to do.. Just find a max date first then check it!
function outdate($dates) {
$dates = str_replace('/','-',$dates);
$dates = explode(',',$dates);
$ts_dates = array();
foreach ($dates as $date) {
$ts_dates[] = strtotime($date);
}
$max_date = max($ts_dates);
$today = strtotime('today');
if($max_date < $today) {
return true;
} else {
return false;
}
}

Daylight Saving Time (DST) in CakePHP

I'm creating a web application using cakephp 1.2.6. There is a functionality that I need to save the time that user is entered in GMT format. I'm using below method to do this.
function convertDateTimeToGMT($dateTimeStr,$fromTimeZone, $format = 'Y-m-d H:i:s') {
if (empty($dateTimeStr))
return $dateTimeStr;
else if (empty($fromTimeZone))
return $dateTimeStr;
else {
// Inverse the + or minus. Decimal value should be passed
//$timeHelper = new TimeHelper();
$newTZ = -1 * $fromTimeZone;
return $this->format($format, $dateTimeStr, null, $newTZ) ;
}
}
function format($format = 'd-m-Y', $date, $invalid = false, $userOffset = null) {
$date = $this->fromString($date, $userOffset);
if ($date === false && $invalid !== false) {
return $invalid;
}
return date($format, $date);
}
function fromString($dateString, $userOffset = null) {
if (empty($dateString)) {
return false;
}
if (is_int($dateString) || is_numeric($dateString)) {
$date = intval($dateString);
} else {
$date = strtotime($dateString);
}
if ($userOffset !== null) {
return $this->convert($date, $userOffset);
}
return $date;
}
function convert($serverTime, $userOffset) {
$serverOffset = $this->serverOffset();
$gmtTime = $serverTime - $serverOffset;
$userTime = $gmtTime + $userOffset * (60*60);
return $userTime;
}
convertDateTimeToGMT($dateTimeStr,$fromTimeZone, $format = 'Y-m-d H:i:s') is the method that I'm calling in my code to pass the date time and time zone. I have a combo box of time zones and if user select time zone as "Pacific" it will pass the -8 as the value of $fromTimeZone. But because of the DST this can be changed.
So is there any way in cakephp to find the up to date time zone values automatically and convert the time to GMT?
Once you know the timezone of the user you can get its offset as follows:
$est_tz = new DateTimeZone('America/New_York');
$d = new DateTime("now", $est_tz);
$offset = $est_tz->getOffset($d);

Categories