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
?>
Related
I've created this function when retrieving a date from database and echo in Italian format to screen:
function get_data_ita($date) {
if ($date == "")
return "";
$d = new DateTime($date);
return $d->format('d/m/Y');
}
where $date is mysql format like: 2017-12-31 14:00:00
Now, if I pass a correct format like: 2017-12-31 14:00:00 the function works.
But sometimes I need to use the SAME function, passing an already formatted date like: 30/12/2017. In this case, i get parsing error of course.
How can I check if date passed is already in Italian format, and if yes return the untouched date, if not, parse the date?
I need a function like:
function get_data_ita($date) {
if ( $date== ALREADY_IN_ITALIAN_FORMAT )
return $date;
if ($date == "")
return "";
$d = new DateTime($date);
return $d->format('d/m/Y');
}
echo get_data_ita("30/12/2017");
echo get_data_ita("2017-12-31 14:00:00");
ECHO:
30/12/2017
31/12/2017
UPDATE: I found solution myself:
function validateDate($date, $format = 'Y-m-d')
{
$d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) == $date;
}
function get_data_ita($datetime_db) {
if ( validateDate($datetime_db, 'd/m/Y') ) {
return $datetime_db;
}
if ($datetime_db == "")
return "";
$date = new DateTime($datetime_db);
return $date->format('d/m/Y');
}
Replace / to -
<?php
function get_data_ita($date) {
if ($date == ""){
return "";
}
$date = str_replace('/','-',$date);
$d = new DateTime($date);
return $d->format('d/m/Y');
}
echo get_data_ita("30/12/2017");
echo "\n";
echo get_data_ita("2017-12-31 14:00:00");
?>
Check demo : https://eval.in/918149
Strtotime returns false if the date is not a valid date format.
This works for your inputs but not if there are other date formats.
function get_data_ita($date) {
if ($date == "") return "";
If(strtotime($date) !== False){
$d = new DateTime($date);
}Else{
$d = DateTime::createFromFormat('d/m/Y', $date);
}
return $d->format('d/m/Y');
}
echo get_data_ita("30/12/2017");
echo get_data_ita("2017-12-31 14:00:00");
https://3v4l.org/kt5Vg
I want to check who is greater datetime between two datetime variables.
I have an string of datetime and i want to convert it into datetime format and check it who is greater?
my variables:
$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';
and I want to check who is greater between above two variables.
if($d1>$d2)
{
return true;
}
There are comparison operators for the DateTime class in php . Something like this :
date_default_timezone_set('Europe/London');
$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
Output
bool(false)
bool(true)
bool(false)
Use strtotime()
$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1>$d2){
return true;
}
You can use DateTime
if (new DateTime($d1) > new DateTime($d2) {
return true;
}
Try strtotime() function like this :
if(strtotime($d1) > strtotime($d2))
{
return true;
}
try this
$d1 = strtotime('2016-02-02 07:35:00');
$d2 = strtotime('2016-02-1 13:10:31');
if($d1 > $d2)
{
return true;
}
You can compare like that:
<?
$d1 = '2016-02-02 07:35:00';
$d2 = '2016-02-1 13:10:31';
$ts1 = strtotime($d1); // 1454394900
$ts2 = strtotime($d2); // 1454328631
if($ts1>$ts2) // 1454394900 > 1454328631 = true
{
echo 1; // return this.
}
else{
echo 0;
}
?>
Use strtotime() for both date and than compare.
<?php
$date = '1994-04-27 11:35:00';
$date1 = '2016-02-10 13:10:31';
$stt = strtotime($date); // 767439300
$stt1 = strtotime($date1); // 1455106231
if($stt>$stt1)
echo 1;
else
echo 0;
?>
use strtotime function to convert string to time formate...
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'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);