Function to Search by date - php

This is function for search by datetime .. i want to search just by date ( yy-mm-dd ) what i need to search just with date withount datetime .
I have tried all the options, but no one makes what I need.
Thank you.

if it is Mysql then you can use DATE function
as something like this
$qb->AndWhere("DATE(m.startDate) = :start");
$qb->setParameter('start',date('Y-m-d'));
for Postgres like this
$qb->AndWhere("m.startDate::date = :start");
$qb->setParameter('start',date('Y-m-d'));
Doctrine have to be setup before https://simukti.net/blog/2012/04/05/how-to-select-year-month-day-in-doctrine2/
$emConfig = $this->getEntityManager()->getConfiguration();
$emConfig->addCustomDatetimeFunction('DATE', 'DoctrineExtensions\Query\Mysql\Date');

Related

Laravel Carbon addHours response modification

I have a booking table where there's a bookingtime datetime field and duration field.
The duration field is integer.
In my get query for the calendar, I am showing the duration so I am trying to :
add the duration to the booking time date.
I am using this in the end of my query:
for ($i=0;$i<count($query);$i++){
$durationdate = Carbon::parse($query[$i]->bookingtime)->addHours($query[$i]->duration);
$query[$i]->end = $durationdate;
}
return $query.
The query is returning everything fine. but the "end" is returning an object
end{ date:"..." , timezone_type:3, timezon: "UTC"}
I want to modify the end to be returned like the other data in my query response as :
end : "2018-02-01 12:00:00" for example
Use the toDateTimeString() method like this:
$query[$i]->end = $durationdate->toDateTimeString();
Or the format() method:
$query[$i]->end = $durationdate->format('Y-m-d H:i:s');
The object returns is of type DateTime, so you can use the format() function to get the expect date 2018-02-01 12:00:00
$query[$i]->end = $durationdate->format('Y-m-d H:i:s');

How to convert ZF2 Doctrine createQueryBuilder date field to timezone

This is my query :
$casesData = $this->getEntityManager()->getRepository('Cases\Entity\Cases')
->createQueryBuilder('cases')
->select('cases.CaseId',
'cases.CreatedDate',
'cases.LeadId',
'cases.InquiryID',
'cases.FirstName',
'cases.LastName',
'cases.CityDoc1',
'cases.StateDoc1',
'cases.SSN',
'cases.Status',
'cases.CaseManagerId',
'users.first_name'
);
cases.CreatedDate is currently have "US Canada" timezone, now I want to convert base on timezone dropdown value, and I can't found any solution to convert timezone as per above query, please help me.
Actually with query builder you can't change the time zone, you have to use loop as doctrine return datetime object than you can modify the time as you wish
foreach($data as item){
$date = $item['CreatedDate']->modify('....')
}

Oracle batch insert with date. How to format date?

I need to batch insert into oracle. But the date is not formatted correctly.
$data[]=array(
'USER_GROUP_ID'=>$wg_id,
'MENU_LINK_ID'=>$id,
'DATA_STATUS'=>1,
'ENTRY_BY'=>$this->session->userdata['rrss_user']['user_id'],
'ENTRY_DATETIME'=>"to_date('".date('d-M-Y')."','dd/mm/yyyy')"
);
Hey I am giving simple example please check this one my be help ...
$this->db->insert_batch()
Generates an insert string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
$user_id = $this->session->userdata['rrss_user']['user_id'];
$data = array(
array(
'USER_GROUP_ID'=>$wg_id,
'MENU_LINK_ID'=>$id,
'DATA_STATUS'=>1,
'ENTRY_BY' => $user_id,
'ENTRY_DATETIME'=> date('d-M-Y')
),
);
$this->db->insert_batch('mytable', $data);
Now the above works fine, but if I try to change the date('d-M-Y') to anything more granular which includes hr/sec/mins
So it looks like since Oracle DATE type uses 'd-M-Y' as the default format, that's all it will accept as a string representation for date field...Would like to know if anyone has any thoughts on how I can set the field with the full date-timestamp, not just 'd-M-Y'.
Without Batch example :
$this->db->set('user_name', $name);
$this->db->set('age', $age);
$this->db->set('date',"to_date('$date','dd/mm/yyyy')",false);
$this->db->insert('mytable');

php zf2 select where clause with SQL function

I need to compare DATE format from a DATETIME.
Prior ZF 2.3.5, the following code was working fine:
$select->where('DATE(arrival_date) <= DATE(NOW())');
$select->where('DATE(departure_date) >= DATE(NOW())');
$select->where('enable = true');
With ZF 2.4.2+ it does not work anymore and produce the following error:
Cannot inherit previously-inherited or override constant TYPE_IDENTIFIER from interface Zend\Db\Sql\Predicate\PredicateInterface
I have tried the following (without error). The problem is that "arrival_date" is a DATETIME format, and I need to compare with a DATE only.
$where = new Zend\Db\Sql\Where();
$where->lessThanOrEqualTo('arrival_date', date('Y-m-d'));
$where->greaterThanOrEqualTo('arrival_date', date('Y-m-d'));
$where->equalTo('enable', true);
$select->where($where);
Ideally, this code should work, but it does not:
$select->where(new Zend\Db\Sql\Predicate\Expression('DATE(arrival_time) <= ?', 'DATE(NOW())'));
I am lost, any idea ?
Just use Expression like this:
$select->where(
array(
new \Zend\Db\Sql\Predicate\Expression("DATE(arrival_date) <= DATE(NOW())")
)
);
No need of ? like you tried. With ? you params will be escaped and it won't work with an expression. Only with params, like a PHP datetime or something like this.
Tested with 2.4.2

MongoDB using timestamps to sort

I have a mongodb that will be storing visitor data. I need to delete the data after ten minutes of not being active and will run a command through a cron. How would I do this?
Currently the collection is setup like so:
{ "_id" : ObjectId("4fd33e0b0feeda3b2406f6be"), "name" : "Dugley Reanimator", "updated" : "Some form of timestmap" }
How should I go about storing a timestamp that I search the collection with I.E for my MySql version:
$sql = mysql_query('DELETE FROM `visitors` WHERE NOW() > DATE_ADD(`last_seen`, INTERVAL 10 MINUTE)');
The ObjectId has a timestamp component to it. see the docs here. This essentially gives you a free insert time that you can use for sorting and querying.
The mongodb drives should give you a way to created an ObjectId off of a timestamp.
In Python:
gen_time = datetime.datetime(2010, 1, 1)
dummy_id = ObjectId.from_datetime(gen_time)
In Java:
Date d = new Date(some timestamp in ms);
ObjectId id = new ObjectId(d)
So once you've created an ObjectId based on "10 minutes ago" you can do a delete query using $lt
in the js console it would be:
db.collectionName.remove({_id: {$lt: <Your Object Id that represents 10 minutes ago>})
The best way to do it (if the timestamp is the same when you insert) its by using the _id field.
The _id field can indicate you the time, and you can do a $lte query to delete old values.
I've written about it here: http://blog.dicarsio.com/post/10739857186/quick-snippet-get-creation-time-from-id-on-mongodb
Your driver will use a MongoDate time (this may map to a more native representation in PHP).
You can then query using something like the following mongo statement:
db.myCollection.find({updated : { $lte : new ISODate("2012-06-09T16:22:50Z") } })
A rough translation for PHP would be:
$search = array(
'updated' => array(
'$lte' => new MongoDate($tenMinutesAgo))
);
$collection->find($search)
Or (Caveat: not tested):
$tenMinutesAgo = new DateTime();
$tenMinutesAgo->modify('-10 minutes');
$search = array('updated' => array('$lte' => $tenMinutesAgo));
$collection->find($search)

Categories