split datetime from array in codeigniter 3 - php

my problem is to get date from array. but there is a time from it.
i get data from model
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
and i want to get datetime
$date4 = $data['result']->tanggal_kembali;
after get the datetime i should split it with explode in php function
$splitTimeStamp = explode(" ",$date4);
and just get the date from it
$date = date('Y-m-d',strtotime($splitTimeStamp));
but the result in view looks like this
enter image description here
so this is full code i wrote
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
$date4 = $data['result']->tanggal_kembali;
$splitTimeStamp = explode(" ",$date4);
$date = date('Y-m-d',strtotime($splitTimeStamp));
just want to split date and time

You need to update the code as the explode() return an array.
Below I have assigned the array parameters to separate variables and you can use them individually.
$data['result'] = $this->MTransaksi->cekpengembalian($id_transaksi);
$date4 = $data['result']->tanggal_kembali;
$splitTimeStamp = explode(" ",$date4);
$dateObject = $splitTimeStamp[0];
$timeObject = $splitTimeStamp[1];
$date = date('Y-m-d',strtotime($dateObject));
NOTE: You didn't mention your $date4 how it looks otherwise could have provided a more concrete answer.
Hope it helps

Related

How to get data from database using date?

I want to selecting and showing data based on current date. I have made query selected for this, but it not working
this is my date data on table :
this is my query:
$now = date('Y-m-d');
$trans = FA_transaction::where('assign_date', $now)->get();
when i execute the query, i don't get any data
You can do :
$trans = FA_transaction::whereDate('date', Carbon::today())->get();
OR if you want to use MySQL's CURDATE function, you can do :
$trans = FA_transaction::->whereRaw('Date(date) = CURDATE()')->get();
You can use whereDate function to filter out records based on date try this!
$now = date('Y-m-d');
$trans = FA_transaction:: whereDate('date','=',$now)->get();
You can use it
$now = Carbon::today();
$trans = FA_transaction::where('assign_date', $now)->get();
OR
$now = Carbon::now()->format('Y-m-d');
$trans = FA_transaction::where('assign_date', $now)->get();

PHP: Merge two date objects into one

I've got two date objects sent from a date input and a time input.
I want to generate a DateTime object with the yyyy-mm-dd from the date input and hh-mm from the time input.
Data sent from date input
[search_from_date] => 2015-08-04T23:00:00.000Z
Data sent from time input
[search_from_time] => 1970-01-01T02:02:00.000Z
I need to merge these two dates into:
2015-08-04 02:02
I've been playing around with exploding the string, etc to no avail,
Any help would be appriciated
Cheers
You can create two DateTime objects from your strings, then use the second to set the time on the first.
$arr = ['search_from_date' => '2015-08-04T23:00:00.000Z', 'search_from_time' => '1970-01-01T02:02:00.000Z'];
$date = new DateTime($arr['search_from_date']);
$time = new DateTime($arr['search_from_time']);
$date->setTime($time->format('H'), $time->format('i'), $time->format('s'));
echo $date->format('r');
Here's an eval.in with an example - https://eval.in/424209
you can split on "T" like that :
$search_from_date = "2015-08-04T23:00:00.000Z";
$search_from_time = "1970-01-01T02:02:00.000Z";
$tab_date = explode("T", $search_from_date);
$tab_time = explode("T", $search_from_time);
$date_time = new DateTime("$tab_date[0]T$tab_time[1]");
var_dump($date_time);
Try this,
$search_from_date = '2015-08-04T23:00:00.000Z';
$search_from_time = '1970-01-01T02:02:00.000Z';
$data = explode('T',$search_from_date);
$data1 = explode('T',$search_from_time);
$data1 = explode('.',$data1[1]);
echo $data[0].' '.$data1[0];
$time=new DateTime('1970-01-01T02:02:00.000Z');
$date=new DateTime('2015-08-04T23:00:00.000Z');
$newDateTime= new DateTime();
$newDateTime->setTime($time->format('H'),$time->format('i'),$time->format('s'));
$newDateTime->setDate($date->format('Y'),$date->format('m'),$date->format('d'));
echo $newDateTime->format('Y/m/d H:i'));
exit;
Something like this should work.
$finalDateD creates a date string for just the Year, month and day
and
$finalDateT creates a date string for just the Hours, minutes
This is then concatenated into the variable $finalDate;
$finalDateD = date('Y-m-d', strtotime($search_from_date));
$finalDateT = date('H:i', strtotime($search_from_time));
$finalDate = $finalDateD.' '.$finalDateT;

addBetweenCondition today not returning

I tried a couple of ways. It's not returning the correct number. It either gives me the default number which is set at the beginning or returns the searched criteria but ignores the store_id.
$percent = 8;
$today = date('Y-m-d');
$criteria = new CDbCriteria();
$criteria->compare('store_id', 253);
// $criteria->condition = 'store_id=:store_id';
// $criteria->params = array('store_id'=>253);
// the bottom addcondition ignores that store id, and gives me what it finds btwn dates
// $criteria->addCondition = 'CURRENT_DATE BETWEEN date_from AND date_to';
// addbetweencondition doesn't find anything between dates
$criteria->addBetweenCondition($today, 'date_from', 'date_to');
$override = Model::model()->find($criteria);
if ($override) {
$override_percent= $override->percent/100;
}
print_r ($override_percent);
You are using addBetweenCondition wrong. The first three parameters should be:
the column to match against
the start date
the end date
I think it should be something like the code below (untested):
$date_from = date('Y-m-d');
$date_to = date('Y-m-d 23:59:59');
$criteria->addBetweenCondition('column_name_that_contains_date', $date_from, $date_to);
Read more about the function:
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addBetweenCondition-detail

PHP displaying date is one day forward after format

When I retrieve a date from a my_sqli query and format it it becomes one day forward. The date is saving correctly to the server, and echoing it before the new format is correct.
$date = "SELECT date FROM blogtable WHERE id = $artID";
$dateEx = mysqli_query($con, $date);
while($dateGet = mysqli_fetch_array($dateEx))
{
//This is in YYYY-mm-dd
$dateGet['date'];//If I echo this, it is correct
}
$source = $dateGet;
$newDate = new DateTime($source);
echo $newDate->format('M-d-Y');
So for example if I tried to use it today(the 24th), it would save correctly, but after the format, display as the 25th.
Wrikken's suggestion worked, changing the statement in the while to $source=$dateGet['date']; and deleting the $source = $dateGet;.

i want to get only date from datetime variable in php

I need to get only date from datetime variable. I was using this code. It was working fine but now I am facing problem when I'm trying to store only date in mysql please. Take a look at the code below and please tell me what I'm missing,
$date_time = "11-Dec-13 8:05:44 AM"
From the date I got from user input, I need to save only date in att_date variable.
$arr = explode("/", $date_time);
$arr2 = explode(" ", $arr[2]);
$att_date = $arr2[0].'-'.$arr[0].'-'.$arr[1];
Here is the solution:
$date_time = "11-Dec-13 8:05:44 AM";
$new_date = date("Y-m-d H:i:s",strtotime($date_time));
Even if you just use "Y-m-d" it will work instead of using "Y-m-d H:i:s",please practice below example:
$datetime = '2022-10-11 06:38:02';
$getOnlyDate = date('Y-m-d',strtotime($datetime));
echo $getOnlyDate; // output: 2022-10-11
You can get the date from the datetime variable using the
Date();
function from mysql using php query, it automatically extracts the date from it. You can learn it here.

Categories