3

I'm relatively new to CF / Flex3 and I've been tasked with making mock applications in order to get my knowledge of the 2 languages up.

I'm creating an application where I require data back 1 week (strtotime equiv '-1 week').

So that the result is always 1 weeks worth.

Whats the comparable equivalent if any for coldfusion? If none, how would I accomplish this task? Just curious, I've searched but cannot find anything yet on this topic.

How I solved this (get data from 'last friday'):

<cfset lastweek = dateAdd("d", -(DayOfWeek(now()) + 1), now()) />


strtotime
Parse about any English textual datetime description into a Unix timestamp

Josh
  • 10,961
  • 11
  • 65
  • 108
Jakub
  • 20,418
  • 8
  • 65
  • 92
  • possible duplicate of [Is there a natural language parser for dates/times in ColdFusion?](http://stackoverflow.com/questions/1003330/is-there-a-natural-language-parser-for-dates-times-in-coldfusion) – nawfal Jan 30 '14 at 11:45
  • Really? after almost 5 years @nawfal you kick this up as a duplicate close request... *sigh* – Jakub Jan 30 '14 at 16:25
  • The problem is with 5 years, or with duplicate choice being wrong one? – nawfal Jan 30 '14 at 16:31

2 Answers2

4

I don't know of a way in coldfusion (not natively anyway) that will take a textual representation of time and do a conversion. A few google searches also did not turn up anything. It could be written but would not be a simple undertaking.

That said, if you want to get a date 1 week back, you could do something like this using the dateadd() function:

<cfset variables.lastweek = dateAdd("w",-1,now()) />

or

<cfset variables.lastweek = dateAdd("d",-7,now()) />

Of course you can substitute now() out for any timestamp or date.

Update:

Remember that because CF is java, you can use any java classes to help you on your way too. It doesn't look like there is a cut and dry equivallent even in java, but these relevant topics may help you on your way:

PHP's strtotime() in Java?

PHP's strtotime() in Java

Community
  • 1
  • 1
Ryan Guill
  • 13,558
  • 4
  • 37
  • 48
  • How would I accomplish this if I needed data from 'last friday' for instance? Thats whats making me scratch my head... – Jakub Sep 28 '09 at 18:44
  • Jakub, use DayOfWeek to get current day, subtract that to get start of week, then subtract two/three days to get the friday. – Peter Boughton Sep 28 '09 at 18:46
  • 1
    @jakub, like @peter boughton says, you would need to do the math to figure out what last friday is. And that does seem like a lot of work, but it will more than likely run faster and be much less prone to error. The problem with language translation like this is that it is "fuzzy", there can be multiple meanings for the same thing. It is a nice thing to have when relying on user input, but in your code it is better to do the appropriate math. – Ryan Guill Sep 28 '09 at 18:53
1

Maybe ParseDateTime will do the job?

Sergey Galashyn
  • 6,946
  • 2
  • 19
  • 39