I am working with the DateTime object and have this problem to obtain the activity of a specific day.
In the controller I do the following query:
$date = new \DateTime('today');
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Result for this query is null, but when I define the parameter date in this way:
$date = new \DateTime('Wednesday, January 14, 2015');
I get the activity that matches this date. Why doesn't today work?
I believe that commenter #prodigitalson is right. When you say today that is in fact now which is formatted with YYYY-MM-DD HH:MM:SS.
Now, if your DBMS column is of type date, DBMS will first normalize two values and only then proceed with comparing.
If your incoming parameter has a value of 2014-01-14 16:47:20, comparing it to DBMS value of 2014-01-14 00:00:00 will no match the record.
Try the following:
$date = new \DateTime(); // no need for explicit `today`
$date->setTime(0,0,0); // reset hours, minutes and seconds to 0
$activity = $em->getRepository('MyBundle:myEntity')->findOneBy(array(
'activity_date' => $date
));
Will this work?
Related
i fetched a number of days from the database to add to an inputed date from my form but it keeps giving a wrong output as date 1970-01-01
$newTime = date('d-m-Y', strtotime($all_leave->date_from.' + '.$leaveDays.' days'));
and for my $leaveDays
$leaveDays = leaveType::all()->where('leave_type','=',$all_leave->leave_type)->pluck('leave_days');
The pluck method retrieves all of the values for a given key. So return array of all values not single value but
strtotime() needs days in number and you are providing array. So it is giving you wrong date. So you need to get first value from array as leavedays .
Just be sure that for leave days you will be only one value, if there are multiple values then you have to iterate if you needed else it will take only first value.
Try this
$leaveDays = leaveType::all()->where('leave_type','=',$all_leave->leave_type)->pluck('leave_days');
$leaveDays = empty($leaveDays[0]) ? 0 : $leaveDays[0];
$newTime = date('d-m-Y', strtotime($all_leave->date_from.' + '.$leaveDays.' days'));
Use Carbon instead of directly data object
// $all_leave->date_from should be in standard mysql datetime format 2012-01-31 00:00:00
$newTime = Carbon::parse($all_leave->date_from)->addDays($leaveDays)->format('d-m-Y');
What is the "cleanest" way to add a date and a time string in PHP?
Albeit having read that DateTime::add expects a DateInterval, I tried
$date = new \DateTime('17.03.2016');
$time = new \DateTime('20:20');
$result = $date->add($time);
Which was no good and returned nothing to $result.
To make a DateInterval from '20:20', I only found very complex solutions...
Maybe I should use timestamps?
$date = strtotime($datestring);
$timeObj = new \DateTime($timestring);
// quirk to only get time in seconds from string date
$time = $timeObj->format('H') * 3600 + $timeObj->format('i') * 60 + $timeObj->format('s');
$datetime = $date+$time;
$result = new \DateTime;
$result->setTimestamp($datetime);
In my case, this returns the desired result, with the correct timezone offset. But what do you think, is this robust? Is there a better way?
If you want to add 20 hours and 20 minutes to a DateTime:
$date = new \DateTime('17.03.2016');
$date->add($new \DateInterval('PT20H20M'));
You do not need to get the result of add(), calling add() on a DateTime object will change it. The return value of add() is the DateTime object itself so you can chain methods.
See DateInterval::__construct to see how to set the intervals.
DateTime (and DateTimeImmutable) has a modify method which you could leverage to modify the time by adding 20 hours and 20 minutes.
Updated
I've included examples for both DateTime and DateTimeImmutable as per the comment made, you don't need to assign the outcome of modify to a variable because it mutates the original object. Whereas DateTimeImmutable creates a new instance and doesn't mutate the original object.
DateTime
<?php
$start = new DateTimeImmutable('2018-10-23 00:00:00');
echo $start->modify('+20 hours +20 minutes')->format('Y-m-d H:i:s');
// 2018-10-23 20:20:00
Using DateTime: https://3v4l.org/6eon8
DateTimeImmutable
<?php
$start = new DateTimeImmutable('2018-10-23 00:00:00');
$datetime = $start->modify('+20 hours +20 minutes');
var_dump($start->format('Y-m-d H:i:s'));
var_dump($datetime->format('Y-m-d H:i:s'));
Output
string(19) "2018-10-23 00:00:00"
string(19) "2018-10-23 20:20:00"
Using DateTimeImmutable: https://3v4l.org/oRehh
I have a table transaction, inside there is column order_on_sale with default value is 0. Then i also have a table config, inside there is column name with two value that is sale_start_date and sale_finish_date. And there are also time columns with values 2018-05-1 18:00 and 2018-05-31 18:00 (YYYY-MM-DD HH: mm).
name and time columns contained in the config table are interconnected,
sale_start_date = 2018-05-1 18:00
sale_finish_date = 2018-05-31 18:00
then when someone orders on sale_start_date and sale_finish_date, the order_on_sale column contained in transaction table will change its value to 1
how do i get the current current time (YYYY-MM-DD HH: mm) to make the change?
$transaksi = new Transaction;
$order_on_sale = 0;
if (Config::get('sale_start_date') && Config::get('sale_finish_date')) {
$order_on_sale = 1;
}
$transaksi->order_on_sale = $order_on_sale;
$transaksi->save();
Below is my code, but I am confused how to get the current date and time if I write such code
thanks for the answers you provide
A bit late answer for questioner but maybe can help someone:
$date = Carbon::now();
$formatedDate = $date->format('Y-m-d H:i:s');
echo($formatedDate);
use the Carbon Library which comes by default with laravel
$date = Carbon::now();// will get you the current date, time
dd($date->format("Y-M-D H:m")); //this will dump the date time in the desired format
Maybe with:
use Carbon\Carbon;
$Now = Carbon::now(new \DateTimeZone('My/TimeZoneifRequired'))->toDateTimeString();
So have you tried using Carbon Class ? as far as i know Laravel 4 supports it.
you can get your current date by doing
$now = Carbon::now()->toDateTimeString();
This will retrieve the current date in the following format YYYY-MM-DD HH:mm:ss (as default)
if you want to get the current date in your mentioned format YYYY-MM-DD HH:mm
by wrapping around Carbon and adding some php functionality you can get so :
$currentTime = Carbon::now()->toTimeString();
$now = Carbon::now()->toDateString() .' '. substr($currentTime, 0, strrpos( $currentTime, ':') ) ;
For further explanation go for the docs : https://carbon.nesbot.com/docs/
simply you can use the below code on the view page.
<p>{{ date('Y-m-d') }}</p>
note: HTML <p> tag is optional.
with hours and minutes
<p>{{ date('Y-m-d H:m') }}</p>
I have 2 tables in my SQL Database. One is called dungeons and the other one is called dungeonruns.
In the dungeons table there is one row called time it's the SQL time format 00:00:00. In the dungeonruns table is a row called stoptime which is in the timestamp format 0000-00-00 00:00:00.
I want my PHP script to get the time from dungeons, add it to the current time and then save it in the stoptime row.
This is what I have tried:
$stoptime = date('Y-m-d H:i:s') + $time;
//$time is the time from the DB and its the 00:00:00 format.
$mysqlDateTime = '2015-09-01 00:00:00';
$timeToAdd = '12:30:00';
$timeParts = explode(':', $timeToAdd);
$intervalString = sprintf('PT%dH%dM%dS', $timeParts[0], $timeParts[1], $timeParts[2]);
$stopTime = (new DateTime($mysqlDateTime))->add(new DateInterval($intervalString))->format('Y-m-d H:i:s');
Demo
Basically, to add that time to that datetime value you need to turn it into a DateInterval which you can then add to a DateTime object.
I want to insert a DateTime object in database where the column type is DateTime. How can I achieve this?
I am using this code:
$cdate = new DateTime('now')
$cd = $cdate->format('d/m/Y h:i:sa')
$udate = new DateTime('72 hours');
$ud = $udate->format('d/m/Y h:i:sa')
$insert = "insert into `winpc_user(mac_address,reg_date,updated_date,status,processor_name,ram_size,os_Name, os_Bits) values('$mac','$cdate','$udate','$stat','$proName','$rSize','$osName','$osBits')"
Same as the comment above, DATETIME's format is:
YYYY-MM-DD HH:MM:SS
Quite straightforward to follow using date()'s format function, it'll share the same with the ->format():
->format('Y-m-d H:i:s');
Sidenote: Of course, this needs to be quoted as well on insertion.
As an alternative, you could also use MySQL functions to achieve the same goal:
NOW()
DATE_ADD(NOW(), INTERVAL 72 HOUR)