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;
}
}
Related
I don't know how to add time
WrokController
if($request->status === 'completed')
{
$t = 0;
$alltime = Work::where('project_id', $project->id)->get();
foreach($alltime as $time)
{
$t = $t + $time->work_time;
}
$project -> fill(['total_work_time' => $t])->save();
}
But isn't there a good way to do this?
You can sum it with interval,
$base_time = new DateTime();
$time_now = clone $base_time;
foreach ($alltime as $time) {
$array = explode(':', $time);
$base_time->add(new DateInterval(sprintf('PT%dH%dM', $array[0], $array[1])));
}
$sum_of_diff = $time_now->diff($base_time);
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;
}
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.
I have to keys, 'payment_date' and 'balance'. I have a function that checks 'payment_date' to see if it's <= 'today', and it works but now I don't know how to fetch 'balance' from that:
function getCurrentBalance($myTable){
$today = new DateTime('now');
$today = $today->format('Y-m-d');
foreach($myTable as $row) {
foreach($row as $key=>$value) {
if ($key == "payment_date" && $value <= $today){
}
}
}
}
You really don't need the second loop, if you already know the keys you need. You can access them directly.
function getCurrentBalance($myTable){
$today = new DateTime('now');
$today = $today->format('Y-m-d');
foreach($myTable as $row) {
if ($row['payment_date'] <= $today){
//Do something with $row['balance']
}
}
}
do it like this instead:
foreach($myTable as $row)
{
if ($row['payment_date'] <= $today)
{
echo $row['balance'];
}
}
I don't understand very well what do you want to do, I think that your answer is:
function getCurrentBalance($myTable){
$today = new DateTime('now');
$today = $today->format('Y-m-d');
foreach($myTable as $row) {
foreach($row as $key=>$value) {
if ($key == "payment_date" && $value <= $today){
$balance = $row['balance'];
...
}
}
}
}
I'm trying to validate date using PHP.
I'd like following formats to be valid:
d/m/yy
d/m/yyyy
dd/m/yy
dd/m/yyyy
d/mm/yy
d/mm/yyyy
dd/mm/yy
dd/mm/yyyy
I've tried many regular expressions and different variations of checkdate() function. Currently I have something like this:
function _date_is_valid($str)
{
if(strlen($str) == 0)
return TRUE;
if(substr_count($str,'/') == 2)
{
if (preg_match("/^((((31\/(0?[13578]|1[02]))|((29|30)\/(0?[1,3-9]|1[0-2])))\/(1[6-9]|[2-9]\d)?\d{2})|(29\/0?2\/(((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))|(0?[1-9]|1\d|2[0-8])\/((0?[1-9])|(1[0-2]))\/((1[6-9]|[2-9]\d)?\d{2}))/", $str))
{
$datearray = explode('/',$str);
if($datearray[2] > 2030)
return FALSE;
return checkdate($datearray[1], $datearray[0], $datearray[2]);
}
else
{
return FALSE;
}
}
return FALSE;
}
This however, validates dates like 11/11/200 and 11/11/200#
How can I validate date to match required format?
Edit: I could check datearray[2] to be between 10 and 30 and 2010 and 2030. But is there a way to check it using regex?
Edit1: return TRUE on strlen($str) == 0 is because I want users to be able to add events without knowing when will the event occur so someone else can qualify the schedule and assign event to certain date later
Just for the record. I ended up doing:
function _date_is_valid($str)
{
if(strlen($str) == 0) //To accept entries without a date
return TRUE;
if(substr_count($str,'/') == 2)
{
list($d,$m,$y) = explode('/',$str);
if(($y >= 10 && $y <= 30) || ($y >= 2010 && $y <= 2030))
{
return checkdate($m,$d,$y);
}
}
return FALSE;
}
Thanks for all your answers
function _date_is_valid($str) {
if (substr_count($str, '/') == 2) {
list($d, $m, $y) = explode('/', $str);
return checkdate($m, $d, sprintf('%04u', $y));
}
return false;
}
If it will always will be date / month / year you can use checkdate:
function _date_is_valid($str)
{
$array = explode('/', $str);
$day = $array[0];
$month = $array[1];
$year = $array[2];
$isDateValid = checkdate($month, $day, $year);
return $isDateValid;
}
an easy way to do this
<?php
if(!$formated_date = date_create_from_format('Y-m-d',$dateVar))
Try this:
function _date_is_valid($str) {
if (strlen($str) == 0) {
return TRUE;
}
return preg_match('/^(\d{1,2})\/(\d{1,2})\/((?:\d{2}){1,2})$/', $str, $match) && checkdate($match[2], $match[1], $match[3]) && $match[2] <= 2030;
}
I have been just changing the martin answer above, which will validate any type of date and return in the format you like.
Just change the format by editing below line of script strftime("10-10-2012", strtotime($dt));
<?php
echo is_date("13/04/10");
function is_date( $str )
{
$flag = strpos($str, '/');
if(intval($flag)<=0){
$stamp = strtotime( $str );
}else {
list($d, $m, $y) = explode('/', $str);
$stamp = strtotime("$d-$m-$y");
}
//var_dump($stamp) ;
if (!is_numeric($stamp))
{
//echo "ho" ;
return "not a date" ;
}
$month = date( 'n', $stamp ); // use n to get date in correct format
$day = date( 'd', $stamp );
$year = date( 'Y', $stamp );
if (checkdate($month, $day, $year))
{
$dt = "$year-$month-$day" ;
return strftime("%d-%b-%Y", strtotime($dt));
//return TRUE;
}else {
return "not a date" ;
}
}
?>
What I did to validate it was simple
function dateValidate($pDate) {
if(empty($pDate))
return true;
$chDate = date('Y-m-d', strtotime($pDate));
if($chDate != '1970-01-01')
return true;
else
return false;
}
You can find it out with using regexp too
how about if(strtotime($str) !== FALSE) $valid = TRUE;