What is the best way to add an expiry date in CakePHP?
I've got an "expiry date" column in my database, I want to add 1 month to the current date, and store this. At the moment, I'm just using strings and plain PHP date functions to create a new date string to save:
$date = date("Y-m-d");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +31 days");
$this->data['Access']['expires'] = $date;
Is there a "more CakePHP" way or efficient/performance wise?
Performance wise we're most probably talking about micro optimizations at best, so that probably shouldn't be your main concern. However, all the back and forth that you're doing there doesn't make much sense, a single call to strtotime() is all you need, ie
$this->data['Access']['expires'] = strtotime('+31 days');
This will result in a timestamp of "now + 31 days".
The Cake-ish way would be to use the CakeTime class, for example CakeTime::fromString(), like
App::uses('CakeTime', 'Utility');
$this->data['Access']['expires'] = CakeTime::fromString('+31 days');
When passing a string (it also accepts integers and \DateTime instances), this is basically just a wrapper for strtotime(), and an additional mechanism that incorporates the timezone set for your application, so while this will be a little bit slower of course, it might still be wise to use it.
See also
Cookbook > Core Libraries > Utilities > CakeTime
Better that adding 31 days is better to use 1 months this will add 30 or 31 days depending on which month will be the current date
$dateAfterOneMonth = date('Y-m-d H:i:s', CakeTime::fromString('+1 months'))
Related
I've just started to use Marketo's mail scripting,
and I need to find out the last of the current month.
I could find the current date as below.How can I do further?
#set($date = $date.calendar)
#set($current_date = $date.format('yyyy-mm-dd', $date.getTime()))
$current_date
Refer to here,
Subtract months from date in velocity
I tried to subtract 1 day from the beginning of next month, but it doesn't work.
Knowing the number of days in this month is also meets the requirement.
The method you tried should work. I think that your problem is that you are overwriting $date - which initially contains the DateTool - with your working variable. When Velocity Tools org.apache.velocity.tools.userCanOverwriteTools configuration value is true (which is the default) Velocity will let you overwrite $date but the DateTool will be unavailable thereafter.
So try changing your working variable to $cal for instance. Then, you have several methods:
#set($cal = $date.calendar)
$cal.add(2, 1)
$cal.set(5, 1)
$cal.add(5, -1)
$date.format('yyyy-MM-dd', $cal)
or
#set($cal = $date.calendar)
$cal.set(5, $cal.getActualMaximum(5))
$date.format('yyyy-MM-dd', $cal)
In all cases, you resort to using the Calendar.MONTH and Calendar.DATE constants (respectively 2 and 5). You may want to put such utility operations in a Java tool of your own to have more readable templates.
I'm working with the Blog app and I see how to filter the Blog posts by year using the Visual Query Designer. I use the querystring value that has the year and in the ValueFilter and my properties are as follows:
Attribute: PublicationMoment
Value: [QueryString:year]-01-01 and [QueryString:year]-12-31
Operation: between
How would I get the posts from a specific month and year, if those values are passed via query string parameters. Because the months of the year have a varying number of days, I'm not sure how you would accomplish this in the Value field of the ValueFilter. Currently I'm passing the 2 digit month as the parameter.
I tried something like: [QueryString:year]-[Querystring:month]
Operation: contains
but the above operation doesn't really work because the datatype is a DateTime object.
I could do it in the razor view but I'm afraid that the paging datasource would have too many pages in it since it would be based on the larger subset of posts for the given year that was passed in the querystring parameter.
Is there any way to do this with the filter?
Basically dates are not perfectly handled yet, but there are a few ways to do it using the visual query:
Use the correct date in the query like between [QueryString:Start] and [QueryString:End] and calculate the correct dates there where you generate the links
Since your main problem with the "between" filter is actually that it would include the last day too, you could also use a two filters a >= first date and another < second date, so the first-date would be the year/month and day 1; the second one is year-month and day 1 as well
Last but not least: if you do it with razor and LINQ you shouldn't run into any performance issues - it's technically the same thing the pipeline does and it's been tested to perform well with tens of thousands of records.
Need to get entities filtering by month instead of complete date values (E.g. Birthdays) using Google App Engine Text Search. On verifying GAE docs, I think it is not possible to query date fields by month directly.
So in order to filter them by month/date, we consider saving each date sub value like Date(DD), Month(MM) and Year(YYYY) as separate NUMBER field along with complete date field.
I verified locally that we can achieve by saving like this. But is this the correct way of saving dates by splitting each field when we want to query on date sub values?
Is there any known/unknown limit on number of fields per document apart from 10GB size limit in GAE Text Search?
Please suggest me.
Thanks,
Naresh
The only time NUMBER or DATE fields make sense is if you need to query on ranges of values. In other cases they are wasteful.
I can't tell from your question exactly what queries you want to run. Are you looking for a (single) specific day of the month (e.g., January 6 -- of any year)? Or just "anything in June (again, without regard to year)"? Or is it a date range: something like January 20 through February 19? Or July 1 through September 30?
If it's a range then NUMBER values may make sense. But if it's just a single specific month, or a single month and day-of-month combination, then you're better off storing month and day as separate ATOM fields.
Anything that looks like a number, but isn't really going to be searched via a numerical range, or done arithmetic on, isn't really a number, and is probably best stored as an ATOM. For example, phone numbers, zip codes (unless you're terribly clever and wanting to do something like "all zip codes in San Francisco look like 941xx" -- but even then if that's what you want to do, you're probably better off just storing the "941" prefix as an ATOM).
i am using salesforce soap api and user getUpdated() method for retrieving updated data but it gives data which is not in timestamp which i gives in method.
$startDate = "2013-09-04 20:51:36"; and
$endDate = "2013-09-05 20:51:36";
Result is
$result = getUpdated("contact", strtotime($startDate), strtotime($endDate)));
but $result also contain data which is having lastModifiedDate="2013-08-19 02:50:00";
what is the reason for this?
there is any mistake in startdate and enddate?
or there is some another way to do this?
The start & end dates are rounded off to minutes (IIRC, down for start & up for end), so the actual date range is slightly larger than the one you specified in this case.
Presumably there's also some TZ offseting going on, which can also account for some of the difference your're seeing (the date/times in the SOAP message typically have the TZ offset in them, so seeing the actual raw dates going back and forth would also help you).
I am looking for the HQL equivalent of converting x amounts days from current timestamp to a queriable value.
So like this sudo-HQL :
from Newspaper as newspaper
where newspaper.published < current_timestamp - days(:daysparam)
And then daysparam is injected as query parameter. And published is date field.
Is this in anyway doable in HQL only, without writing your own hibernate dialect or using criteria in actual code? It seems such as standard feature to not be supported by plain HQL seems strange.
I am using Spring batch's HibernatePagingItemReader which is xml only, so I wanted to avoid the yakshaving of extending that class or creating my own custom dialect etc.
Similar question seems to only suggest calendar critera or new dialect:
Performing Date/Time Math In HQL?
How to perform date operations in hibernate HQL
Something doesn't look quite right: you said that published is a date field (in the database, I suppose). Then, you are using current_timestamp minus an integer value to compare with the date field. As result, you are not getting the timestamp for the date in the parameter, you are just getting current_timestamp - 2, which I don't believe represents "two days ago" ;-) If you have used current_date, I guess it might work.
from Newspaper as newspaper where newspaper.published < current_date - :daysparam
But still, I'd prefer to leave this calculation to the Java side, so that the query would be:
from Newspaper as newspaper where newspaper.published < :start_date
This won't work only if you are not using UTC in your servers (which you should).