How to make date comparison in yii framework - date

I am creating project in yii. I am having Poll table with the fields as-
-pollId
-pollQustion
-isActive
-publishDate
I want to check weather publishdate of poll is not greater than current date.
I am implementing as-
$CurrrentDate=new CDbExpression('NOW()');
if($record->publishDate < $CurrrentDate))
{
some code......
}
But its not executing correctly. Code is executing even if publish date is greater than current date. So how to make this comparison in yii framework. please help me

PHP do not have a date format. When you compare $CurrentDate and $record->publishDate you compare strings. Well, for right compare you need to convert it to Unix's timestamp
// here you set right date/time pattern
// You need to explain CDateTimeParser for your pattern
// just easy use `strtotime` if you have 2012-12-12 12:12:12
$publishTimestamp = strtotime($record->publishDate);
// $publishTimestamp = CDateTimeParser::parse($record->publishDate, 'yyyy-MM-dd hh:mm:ss');
// if you need to use Hours and minutes just easy use `time()`
$currentDateTimestamp = time();
// $currentDateTimestamp = strtotime(date('Y-m-d H:i:s'));
// now we can compare
if ($publishTimestamp < $currentDateTimestamp) {
// do some
}

Related

Converting timestamp object to date with difference in flutter

Let’s say I have an object of type Timestamp set to 2 days from now.
How to get the difference in time and then format it in DateFormat('hh : mm : ss') in dart ?
Construct a DateTime from the Timestamp.
Subtract from that DateTime the initial time (which I presume should be DateTime.now()) to get a Duration.
Format the Duration. Its default .toString() implementation uses hh:mm:ss.microseconds, which is close to what you want. If you want more control, you can easily format it yourself.
var dateTime = DateTime.fromMicrosecondsSinceEpoch(timestamp.timestamp!);
var duration = dateTime.difference(DateTime.now());
print(duration);

AppSync subtract/add day to current day in request mapping template

In my AppSync mapping request template, I need to add or subtract day from the current date.
All I could find are just formatting and parsing time helpers: Time Helpers in $util.time
#set( $todayString = $util.time.nowISO8601())
todayString is then like: 2019-08-23T09:00:00.000Z but I need to set new variables representing same time but one day prior current date or one day after it with the same formatting.
Is it possible at all using only vtl - mapping request template for my DynamoDB datasource?
I found a solution by using epoch time utils. Since it returns long, we can manipulate dates like in this sample and then convert it back to ISO8601 using existing time helper which accepts long value and returns formatted one further needed.
#set( $currentTimeEpoch = $util.time.nowEpochMilliSeconds())
#set( $fromStartEpoch = $currentTimeEpoch + (1000 * 60 * 60 * 24))
#set( $currentTime = $util.time.epochMilliSecondsToISO8601($currentTimeEpoch))
#set( $fromStart = $util.time.epochMilliSecondsToISO8601($fromStartEpoch))

MomentJS date string adds one day

I don't understand why this date is saved as +1 day:
startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017
But if I do:
startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017
Any ideas?
Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/
Thanks!
If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.
Add utc() to avoid it.
moment(startdate).utc().format('DD-MM-YYYY')
or
moment.utc(startdate).format('DD-MM-YYYY')
If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()
Late to the party on this one, but I did just convert a few of our product's date-time objects to https://moment.github.io/luxon/
Takes out the need for the .utc() method above.

How do I make a date representation ('1/1/2014') from = 3-1-2014 5:50:46, Function loadDatabaseFromSheet()

I am new to Appsript Script.Db
We have a spreadsheet whit about 300 row's of date in the first column date : 3-1-2014 5:50:46.
When we do a function loadDatabaseFromSheet() we get the error date Timestamp is not a number.
ScriptDB cannot store Date objects directly; instead, you must store a representation of the >date and reconstruct it later. If you don't intend to search based on dates, then you can >store the numeric timestamp from the Date object like this:
var date = new Date('1/1/2014');
var item = {
timestamp: date.getTime();
}
var record = db.save(item);
Do we have change all the date by hand in a proper way from 3-1-2014 5:50:46 to '3/1/2014'?
Hope that there is a better way to get this done?
// How and where can i make a representation: ('1/1/2014'); I hope i don't have to change
// all the date's in the spreadsheet by hand ?My spreadsheet has 300 Row's, first collumn =
The script DB cannot save Date datatype by default, so what you need to do is covert it to its time representation and save it to DB as the above info suggests.
As for your question regarding the need to change date format, it will not be necessary just do a (new Date(value)).getTime() at the time of saving to the DB. Where value represents the datetime in the first column.

Jasper Reports - Add one day to a Date Parameter

I'm creating a Jasper report that includes the following parameters:
DATESTART (Date)
DATEEND (Date)
These parameters indicate a date range for a field called DATECREATED (Timestamp) which includes times. I would like the date range to be INCLUSIVE, that is, if I filter for "Jan 1, 2009" to "Jan 31, 2009", any DATECREATED value on Jan 31, 2009 (such as "Jan 31, 2009 15:00") will be included in the report.
When I used Crystal Reports in the past, I used the DATEADD function to create a filter expression like the following:
{DATECREATED} >= {DATESTART} and {DATECREATED} < DATEADD("d", 1, {DATEEND})
(I realize that this isn't syntactically correct, but you get the idea.)
Is there any way to do something similar in Jasper Reports?
If you understand French, there the same question is asked in this thread
(the only difference is that it is about adding a month)
The proposed solutions are following:
SQL
Do it with SQL statement directly in the query (if your data source is a SQL datasource of course).
With MySQL you can do something like
DATE_ADD($P{DATEEND},INTERVAL 1 DAY);
more information: Date and Time Functions (MySQL doc)
JAVA
The other solution is to use the Java possibly of the Date object:
I proposed something like :
$P{DATEEND}.setDay($P{DATEEND}.getDay()+1)
But I did not try it (and it is probably wrong).
Maybe you need to defined a new Date Variable DATEEND_1
with a value expression like :
new Date($P{DATEEND}.getTime() + 24*60*60*1000)
or
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)
And use this new variable in your query V{DATEEND_1}.
(again I am not sure of it)
Try this:
new java.util.Date($P{DATEEND}.getTime() + 24*60*60*1000)
Another option is to use the Groovy SDK that comes bundled with the latest versions.
new Date().plus(1) //Today plus one day = tomorrow.
Or to be more complete - add your two parameters DATESTART and DATEEND and set the default expression to be the above code. Then in your query add the following to the where clause:
DATECREATED >= $P{DATESTART} and DATECREATED < $P{DATEEND}
or depending on your SQL variant.
DATECREATED BETWEEN $P{DATESTART} AND $P{DATEEND}
Suppose you have a Parameter PARAM1 and you want to add 366 days to param1 then you can do it by following way
1) Declare another parameter say $P{finalDate}
2) Code like below for $P{finalDate} as the Default Value Expression
new Date($F{PARAM1}.getTime() + 366L*24*60*60*1000)
Dont forget to put L after 366 . Without putting L , it may not work properly and may not give accurate date.
It will add 366 days to PARAM1
We have built a library of static date-related functions, which can be used like this:
DateUtil.add(NOW(), 0, 0, 1)
The above would get you a date one day into the future (the arguments are year, month, day). It would be nice to have a DATE_ADD function supported directly by JasperReports. Sounds like a patch waiting to be submitted.
DAYSINMONTH($P{Date}) >= (DAY($P{Date})+ 1)
?
DATE(YEAR($P{Date}),MONTH($P{Date}),DAY($P{Date})+1)
:
((MONTH($P{Date}) + 1) > 12) ? DATE(YEAR($P{Date}) + 1,1,1) : DATE(YEAR($P{Date}),MONTH($P{Date}) +1 ,1)
I am sorry for necro'ing this post but I thought that I should share this as another alternative to the options posted above. You just need to change all the $P{Date} to be your parameter!
You can create a class to help you, which has a static method which processes your parameters (or modify class JasperUtils by adding a static method). This way you can have amethod returning a boolean which will act exactly as the filter you need.
package com.package_name.utils;
public class JasperUtils2 {
public static Boolean filterDate(Date dateStart, Date dateEnd, Date dateCreated) {
if (dateCreated.compareTo(dateStart) < 0) return false; // dateCreated is greater or
// equal to dateStart
if (dateCreated.compareTo(dateEnd) > 0) return false; // dateCreated is smaller or
// equal to dateEnd
// you can combine the two conditions into one. I wrote it like this to be more obvious
}
}
To use this method, you need to import the class created (Edit -> Report import directives -> new import ---- this may differ for different versions of iReport). Then, you can use the method in static way:
JasperUtils2.filterDate(..., ..., ...)