getting error with zend 2 and DateTime object - zend-framework2

I'm trying get current date in zend 2 , I use this code to get current date and time
$datetime = new DateTime();
But it throw an error "Class 'Application\Controller\DateTime' not found in C:\wamp\www\zf2\module\Application\src\Application\Controller\IndexController.php on line 26
how can I fix this bug? thank you so much :)

$datetime = new \DateTime("now");
or if you dont like the backslash include
use DateTime;
at the start of your file and then the format is;
$datetime = new DateTime("now");

Related

Why do I get "fb conversion error from string "0"" error when using a normal query?

I'm new to Delphi and Firebird. When I run the following query, I get an error:
fb conversion error from string "0"
RECEIPTDATE is a DATE column.
How should I change my query?
FDQueryDataBetween.SQL.Add('select * from RAPOR where RECEIPTDATE BETWEEN '+startDate+' AND '+stopDate+'');
I solved the issue with using parameters.
FDQueryDataBetween.SQL.Add('select * from RAPOR where RECEIPTDATE BETWEEN :startDate AND :stopDate');
FDQueryDataBetween.ParamByName('startDate').AsDate:= DateTimePickerStart.Date;
FDQueryDataBetween.ParamByName('stopDate').AsDate:= DateTimePickerEnd.Date;

Date.strptime throwing invalid date for "%Y%W" format

My date = 1st January, 2015.
I did
date_formatted = date.strftime('%Y%W') which returned "201500"
Then when am trying to do Date.strptime(date_formatted, '%Y%W') it's throwing an invalid date error even though the original string was returned from strptime's counterpart srftime.
Found the solution. Time.strptime(date_formatted, '%Y%W') is what will work here.

How to get the Current Date in ReactNative?

I am building my first ReactNative iOS and Android app. I am an iOS coder with Swift and Obj-C. How do I fetch the current date using ReactNative.
Shall I use Native Modules or is there an ReactNative API from which I can get this data ?
The JavaScript runtime in React Native has access to date information just like any other environment. As such, simply:
new Date()
... will give you the current date. Here's the MDN on working with dates in JS.
If you want a good JS library to make working with dates even easier, check out MomentJS.
I used {new Date()} was generating Invariant Violation: Objects are not valid error the following date function worked for me.
const getCurrentDate=()=>{
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
//Alert.alert(date + '-' + month + '-' + year);
// You can turn it in to your desired format
return date + '-' + month + '-' + year;//format: d-m-y;
}
For more info https://reactnativecode.com/get-current-date-react-native-android-ios-example
It works for ios as well.
I think this should work;
new Date().toLocaleString()
It will return date and time formatted in your local format (usually in the below format);
"6/22/2021, 2:59:00 PM"
if you get currentmilisec date use + new Date() and if get current use new Date()
I hope Help to you
GoodLuk

Converting a string into a date Ruby on Rails

I'm attempting to convert a string date into a date that can be stored in the database. These are my attempts:
params[:event][:start_date] = '03-21-2016'
DateTime.strptime(params[:event][:start_date], '%Y-%m-%dT%H:%M:%S%z')
or
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y')
However I keep getting an invalid date error. I'm not sure what I'm doing wrong.
One way to do it would be this:
date_string = '03-21-2016'
month, day, year = date_string.split('-').map(&:to_i)
DateTime.new(year, month, day)
You need to understand what each fragment (eg %Y) in the format string ('%Y-%m-%dT%H:%M:%S%z') means: read this.
http://apidock.com/rails/ActiveSupport/TimeWithZone/strftime
Once you know this, you can tailor a format string to the date string you have, or expect to get: in this case, "%m-%d-%Y".
When debugging create a new, basic and simple, version of the code and test that.
require 'date'
params = '03-21-2016'
DateTime.strptime(params, '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>
Note the order for the format: '%m-%d-%Y', which works. That's the problem with your first attempt, where you tried to use%Y-%m-%d. There is NO month21or day2016`.
Your second attempt is valid but your question makes it appear it doesn't work. You need to be more careful with your testing:
params = {event:{start_date:'03-21-2016'}}
DateTime.strptime(params[:event][:start_date], '%m-%d-%Y') # => #<DateTime: 2016-03-21T00:00:00+00:00 ((2457469j,0s,0n),+0s,2299161j)>

Saving and Querying a Date in GORM Grails

I have a database table TableA, which has a column 'theDate' for which the datatype in the database is DATE.
When I save a java.util.Date to 'theDate' through GORM it appears to save just the date value when I look at the data in the table by just executing select * from TableA.
However, when I run a query such as:
select * from TableA where theDate = :myDate
No results are found, but if I run something like;
select * from TableA where theDate <= :myDate
I do get results.
So it's like the Time is relevant.
My question is how do I save a Date and query for a Date ignoring the Time completely and just matching on an exact Date only?
Thanks.
note: I have also tried using sql.Date and util.Calendar but to no success.
clearTime()
You can use clearTime() before saving and before comparing to zero out the time fields:
// zero the time when saving
new MyDomain(theDate: new Date().clearTime()).save()
// zero the target time before comparing
def now = new Date().clearTime()
MyDomain.findAll('SELECT * FROM MyDomain WHERE theDate = :myDate', [myDate: now])
joda-time plugin
An alternative would be to install the joda-time plugin and use the LocalDate type (which only holds date information, no times) instead of Date. For what it's worth, I don't think I've worked on a project with dates without using the Joda plugin. It's completely worth it.
If you have date saved without clearing you could retrieve it using range, as Jordan H. wrote but in more simple way.
def getResults(Date date) {
def from = date.clearTime()
def to = from + 1
def results = MyDomain.findAll("from MyDomain where dateCreated between :start and :stop" ,[start:from,stop:to])
}
Your question may be a duplicate. See Convert datetime in to date. But if anyone has more recent information, that would be great.
If that doesn't help, you can hack it the way I might, with a BETWEEN restriction, e.g.
def today = new Date()
def ymdFmt = new java.text.SimpleDateFormat("yyyy-MM-dd")
def dateYmd = ymdFmt.format(today)
def dateTimeFormat = new java.text.SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
def startDate = dateTimeFormat.parse("${dateYmd} 00:00:00");
def endDate = dateTimeFormat.parse("${dateYmd} 23:59:59");
MyDomain.findAll("from MyDomain where dateCreated between ? and ?", [startDate, endDate])
It's definitely not pretty, but it may get you where you're going.
I figured it out.
I used DateGroovyMethods.clearTime to clear the time value before saving.
You can use the DB type date not datetime , in the filed type

Resources