I need to set a dateTime in this specific date format:
2016-10-19T11:06:20.000+00:00
So I found that this one work:
'date'=> $dateStart->format('Y-m-d\Th:m:s')
this is from a array cell setting inside a loop. At the first level of an multi level array.
Before this set, $dateStart is treathed like that:
$dateStart = new DateTime();
$dateStart->setTimeStamp($inputs['extraData']['dataStart']/1000);
And the var $inputs['extraData']['dataStart'] contain an UnixTimeStamp as this one:
1493828407000
All the time stamp are correct in the right ascending order.
In a next level of the same array I try to set another timestamp in this way:
$timeStamp=$cellaViaggio['timestamp'];
$date3= new DateTime();
$date3->setTimestamp($timeStamp/1000)->format('Y-m-d\Th:m:s');
then inside the array
array('name'=>'TIMESTAMP','value'=>$date3)
then this array is used as soap Data envelope in a call made by PHP Soap client.
If I leave all like that my Laravel rise an error of:
ErrorException in TripController.php line 37:
Object of class DateTime could not be converted to string
Otherwise if I leave all like that but I move the format command ( ->format('Y-m-d\Th:m:s') ) inside the array cell writing like that:
array('name'=>'TIMESTAMP','value'=>$date3->format('Y-m-d\Th:m:s'))
this no raise any error but the timestamp is wrong, because the minutes will not change and just second are increased at every loop, but not the minutes!
So something like about the time:
00:01:30
00:01:40
00:01:59
00:01:33
oo:01:03
As I said we talk about progressive timestamp.
Keep in mind that the timestamp is assigned to an Array of Array of Array to keep the soap envelope in the right format for the server.
this is the code for the SOAP client:
$client = new \SoapClient("http://soapserviceserver.xx/services/spPushDataServicePort?wsdl");
try {
return $cli->pushData($param);
} catch (SoapFault $e) {
return $e->getMessage();
}
$params contains the array set as described above.
The format is not proper because you are using
$date3->format('Y-m-d\Th:m:s')
Instead you should use
$date3->format('Y-m-d\Th:i:s')
Here is an example
\Carbon\Carbon::now()->format('Y-m-d\Th:i:s')
Result
2017-06-13T06:44:45
result after some time 2017-06-13T06:45:24
Read more about PHP Date formats here
i : Minutes with leading zeros
If you want perfect Date format according to the example given in the question then you can use
date(DATE_ATOM)
Result 2017-06-13T18:51:08+05:30
Related
I am working with neo4j in PHP and I need to have a DateTime field that allows to store the time zone. If I save it as a string it is more difficult to make queries.
Add the timezone, and just output the string like this:
$x = new DateTime('2018-09-18 22:00:00', new DateTimeZone('Europe/Brussels'));
echo $x->format(DateTime::W3C);
Which gives you:
2018-09-18T22:00:00+02:00
When you pull your time from the DB, you can now create the object like this:
DateTime::createFromFormat(DateTime::W3C, $row['your_date_column']);
Play with it here: https://3v4l.org/DQjJO
I need to delete a mongodb element using php based on the date of the element
When i fetch the Date of the element without any formatting or conversion i get this as an output
0.52400000
I dont know which format this is but by using
date('Y-m-dH:i:s', $post["Date"]->sec)
this function i convert the date to human readable format which gives me something like this
2017-05-1210:23:022017-05-1210:38:102017-05-1210:24:58
now based on this value i want to delete an item in the mongodb collection... i.e that specific item which is having this timestamp..
Try this,
$date = new MongoDate(strtotime("2017-05-12 10:24:58"));
$collection->remove(array('Date' => $date));
Please refer these links - MongoDate and mongodb remove elements. And use MongoDate class all the time. Otherwise there can be conflicts.
EDIT
You can get your date objects' getTimestamp() insteads of strtotime("2017-05-12 10:24:58")
I have a variable pulled from a SQL DB which is in the "datetime" format of:
2017-02-22 16:24:12
I will be needing to find a file in this directory based on the results of the remaining code, but im not sure how to format the date variables for the directory.
Eg:
/basefolder/2017/02/22/File.extension
I have read lots about using date variables, but they are all based on the current date. My thoughts were that I could just strip the numbers like
/basefolder/"first 4 numbers"/"second 2 numbers"/"last 2 numbers"/
VS trying to do this with any sort of date/time functions. But perhaps there is a date fuction I can/should use.
I do know that the datetime will always be formatted as such, but it feels "hackish" to do it the first way. I could do it in bash in a second, but I'm not so good w/ PHP.
Thanks for any help!
PS: To give context, I will be getting 2 variables passed to this php script, phone number and date. I will then query the CDR table for the Unique ID of this call record based on that information, then I know the file will be in:
/monitor/2017/02/22/*uniqueID*
Which I will then pass back to the original script to have it download the file.
You can use the function strtotime to convert your date string to a timestamp, and then use the date function to create the date formatted as you like, and you can pass the timestamp as the second argument (it only uses current time if you pass only the format)
docs for strtotime: http://php.net/manual/en/function.strtotime.php
<?php
$dateString = '2017-02-22 16:24:12';
$timestamp = strtotime($dateString);
$datepath = date('Y/m/d',$timestamp);
$path = "/basefolder/$datepath/";
var_dump($path);
This outputs: string(20) "/basefolder/2017/02/22/"
This code could be shortened to:
$path = "/basefolder/".date('Y/m/d/',strtotime('2017-02-22 16:24:12'));
It sounds like you already found the date function but there is more info on it here: http://php.net/manual/en/function.date.php
Note that you have to do it in two stages, as if you include the /basefolder/ part in the format passed to date, then it will replace individual characters with various formats of the date - e.g.:
$datepath = date('/basefolder/Y/m/d',$timestamp);
Sets $datepath to something like bpm12US/Pacificf2017Wednesday22US/PacificWed, 22 Feb 2017 16:24:12 -0800/2017/02/22
b is left alone because it means nothing, but then a is replaced with pm because of the time, s is replaced with the seconds value, e is replaced with the time zone and so on.
date_parse_from_format() will convert from string to broken down time array:
<?php
$d = date_parse_from_format("Y-m-d H:i:s", "2017-02-22 16:24:12");
$ds = sprintf("/base/%d/%02d/%d", $d['year'], $d['month'], $d['day']);
echo $ds;
This is my final code snippit!
<?php
$dateString = '2017-02-22 16:24:12';
$base = "/monitor/";
$path = "$base".date('Y/m/d/',strtotime("$dateString"));
echo $path;
?>
How can i use the format methode in the Class DateTime in PHP and take some format like :
'Y-m-d H:i' or just the date 'Y-m-d'.
this my code and i putted a picture what i take when i do the dump :
$thisday = new \DateTime(); // I want to take the system date is it ok this instruction ?
$thisday->format('Y-m-d');
dump($thisday);
die();
How can i take just the 2016-04-21 ?
Or take the date, hour and minutes ?
format() does not transform the original DateTime object into your formatted result, it simply returns your formatted result, which you are not assigning to anything. You need to assign
$thisday->format('Y-m-d');
to a new variable, and use that.
You need to assign the result a variable. DateTime::format is returning the value, not changing the object itself
$str = $thisday->format('Y-m-d');
dump($str);
Hi I have a problem where I am trying to get a total time that a job has taken (using database fields job_start and job_end time(7))
then adding this to another model's field,
the code i have is
if (isset($_POST['Jobs'])) {
$model->attributes = $_POST['Jobs'];
$model->setScenario('closejob');
$model->status = 2; //set status to closed
//date time difference - this is the part I need help with
$diff = $model->job_start - $model->job_end;
//need to get customer model and add time diff to it
$customermodel = Customers::model()->findByPk($model->customer_ID);
$customermodel->total_time = $customermodel->total_time + $diff;
$customermodel->save();
if ($model->save())
$this->redirect(array('view', 'id' => $model->job_ID));
}
I have tried string to time and other date functions but to no avail , the above code throws the following error
CDbCommand failed to execute the SQL statement: SQLSTATE[22007]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Conversion failed when converting date and/or time from character string..
Any ideas on the proper way to do this ?
i.e am I going about the adding of time and calculating the differences all wrong ?
I think it has something to do with converting the string to a time format but I am unsure how to do this
Maybe you have to do
$diff = $model->job_end - $model->job_start;
Instead of
$diff = $model->job_start - $model->job_end;
Unless you have an attached behavior for that model, it is likely that your job_start and job_end attributes are strings that are formatted according to MSSQL, assuming the column types are a date/time representation. This is certainly the way CActiveRecord models work with MySQL and Yii with DATETIME type.
To do differences, you would need to convert first. Take a look at CDateFormatter and use it to convert your attributes to numerical representations that allow for arithmetic.
If you cannot get Xdebug to work, then invest time in getting the Yii::log() function working (it will output to runtime/application.log by default.