Kartik Datetime picker yii2 - yii2

I am using Kartik Date picker.
in database I am using timestamp. but checkout value is not set. it shows (not set)

Before save convert to timestamp
$date = date('d-m-Y g:i A');
$dateTimeObject = \DateTime::createFromFormat('d-m-Y g:i A', $date);
$timestamp = $dateTimeObject->getTimeStamp();
When read convert timestamp to datetime
$getDate = new \DateTime();
$getDate->setTimestamp($timestamp);
$date = $getDate->format('d-m-Y g:i A');

Related

yii2, turn existing unix values into an timestamp inside the query

Working on an exciting project which as columns with Unix timestamp instead of a timestamp, so I need to make these values readable timestamps, is there a way to do this inside the query itself?
$query = (new \yii\db\Query())
->select(['a.created_at',
'a.updated_at',
])
->from(['a' => 'product'])
->all();
// the idea
'created_at' => date('Y-m-d H:i:s')
This should work for you
$query = (new \yii\db\Query())
->select([new \yii\db\Expression('from_unixtime(a.updated_at) as updatedAt,
from_unixtime(a.created_at) as createdAt')])
->from(['a' => 'product'])
->all();
or you can use FROM_UNIXTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x'); to create a desired format like 2015 13th November 10:08:01 2015. see HERE for more details.

convert a data varchar to date month

I have a table with a date column in it, but its data type using a varchar.
Month
| 01 02 03
I want to convert that data into a form of month name,I have tried this way, the data is successfully converted into months but always month of january.
foreach($list as $post) {
$time = strtotime($post->album_periode_bulan);
$newformat = date('F',$time);
$no++;
$row = array();
$row[] = $no;
$row[] = $time;
$data[] = $row;
}
I have implemented this in datatables
In MySQL you do
UPDATE your_table
SET your_column = DATE_FORMAT(STR_TO_DATE(your_column, '%m'), '%M');
and that's it. Read more about the format parameters for both functions here.
Note though, newer releases set the sql mode NO_ZERO_IN_DATE by default. You might need to disable this temporarily with
set session sql_mode = "";
Check this before!!! with
show session variables like 'sql_mode';
or you might end with all your rows being NULL.

PHP Mysql PDO retrieve records by date range

I'm retrieving a list of items from my database using the PDO approach and when I do:
$select['from'] = date('Y-m-d 00:00:00',strtotime("2016-02-01"));
$sql = "SELECT * FROM listings WHERE date_added >= :date AND date_added < DATE_ADD(:date, INTERVAL 30 DAY)";
$statement = $pdo->prepare($sql);
$statement->bindParam(":date", $select['from'], PDO::PARAM_STR);
$statement->execute();
var_dump of my result gives:
object(stdClass)#5 (2) { ["data"]=> array(0) { } ["rows"]=> int(0) }
Running the same query directly through PhpMyAdmin gives the desired result. date_added is data type DATETIME
This can be a problem of your date format.
You have to use the format 'Y-m-d H:i:s' in PHP.
$date = date('Y-m-d H:i:s', strtotime($yourdate));

Make Zend\Db (zf2) between clause inclusive

I'm currently using Zend Framework 2 and a query with date ranges to obtain data out of a MySQL DB, and I came across the between clause that was previously not available in ZF1.
However, my code which looks something like this is not working correctly:
$dateStart = '2012-12-20';
$dateEnd = '2012-12-31';
$sql = new Sql($_db);
$select = $sql->select()
->from(array("t" => $table))
->columns(array("col1" => "col_as_1", "col2" => "col_as_2"));
$select->where->between("date", $dateStart, $dateEnd);
$stmt = $sql->prepareStatementForSqlObject($select);
$result = $stmt->execute()->getResource()->fetchAll(\PDO::FETCH_ASSOC);
Apparently the between clause is not inclusive, I can only get results until 2012-12-30, is there a way to make it inclusive? I've been taking a look at the ZF2 docs but they are not very helpful and running the same query on MySQL query browser returns all of the data I need.
So you can try lessThanOrEqualTo and greaterThanOrEqualTo.
Between doesn't seem to provide this functionality: between($identifier, $minValue, $maxValue)
If you trace out your query with $select->__toString() you can see the query as string.
I don't have ZF2 on my computer but I could imagine that between in ZF2 will output date > '2012-12-20' AND date < '2012-12-31'.
NOTE THIS : WHEN USING between on Mysql
date_column_name between 'startDate' AND 'endDate'
NOTE : you should want to insert +1 date to endDate . Because of when you insert 2015-05-18 date to endDate.you can not get data of 2015-05-18.So you need to plus one date to endDate.
You can do it using this
$plusToDate = date('Y-m-d H:i:s', strtotime($toDate . ' + 1 day'));
The BETWEEN should be inclusive, are you sure there are no hours, minutes and seconds after the date, that would cause it not to select dates on 2012-12-31 since 2012-12-31 00:00:01 would technically be > 2012-12-31
Format must be same, use mysql DATE function
$select->where->between("DATE(date)", DATE('.$dateStart.'), DATE('.$dateEnd.'));
$from_date = date('Y-m-d', strtotime($AppCleaner->from_date ));
$to_date = date('Y-m-d', strtotime($AppCleaner->to_date ));
$select->where->between('appointment_date', $from_date . ' 00:00:00', $to_date . ' 23:59:59');
also, Use the between clause as below:
$sql = new Sql($this->adapter);
$select = $sql->select();
$select->from('app_posts');
$select->where->NEST->between( 'id', 30,40);
$select->group('app_posts.id');
// echo $select->getSqlString($this->adapter->getPlatform());
// exit;
$statement = $sql->prepareStatementForSqlObject($select);
$result = $statement->execute();
$resultSet = new ResultSet();
$resultSet->initialize($result);
$posts = $resultSet->buffer()->toArray();
return $resultSet;
Try this:
//I have a static function to make conversion Data Format
public static function convertBrazilianDate2MySQLDatabase($dataBrazil) {
$array = explode("/", $dataBrazil);
$array = array_reverse($array);
$str = implode($array, "/");
return date("Y-m-d", strtotime($str));
}
//In My Service I built my sentence
$dtStart = \Estrutura\Helpers\Data::convertBrazilianDate2MySQLDatabase($dt_start) . ' 00:00:01';
$dtEnd = \Estrutura\Helpers\Data::convertBrazilianDate2MySQLDatabase($dt_end) . ' 23:59:59';
$select->where->between('field name in table', $dtStart, $dtEnd);
[...]

xml unix timestamp to mysql timestamp and record mysql

My XML:
<simpletype type="TURKEY">
<city code="01">
<telcode>(0 322)</telcode>
<name>Adana</name>
<time>1345337940</time>
</city>
<city code="02">
<telcode>(0 416)</telcode>
<name>Adıyaman</name>
<time>1340236800</time>
</city>
</simpletype>
MY Mysql import Code:
require_once('db.php');
#mysql_connect(_SERVER,_USER,_PASSWORD);
#mysql_select_db(_DATABASE);
$doc = new DOMDocument();
$doc->load( 'CityCode.xml' );
$cities = $doc->getElementsByTagName( "city" );
foreach( $cities as $city )
{
$names = $city->getElementsByTagName( "name" );
$codes = $city->getElementsByTagName( "telcode" );
$times = $city->getElementsByTagName ( "time");
$name = $names->item(0)->nodeValue;
$code = $codes->item(0)->nodeValue;
$time = $times->item(0)->nodeValue;
mysql_query("SET CHARACTER SET utf8");
$sql = "INSERT INTO telcode (sehir, name, time) VALUES ('$name' ,'$code' ,'UNIX_TIMESTAMP ($time') ";
#mysql_query($sql);
}
My Question:
My xml source have a unix timestamp and i must use is mysql timeststamp (0000-00-00 00:00:00)
How can i convert $time unix timestamp to mysql timestamp and record mysql db ?
In MySQL you can convert a UNIX-style timestamp (seconds since 1970 UTC) to a MySQL timestamp with the FROM_UNIXTIME function.
See here. http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_from-unixtime
In php, you can read a timestamp out into whatever format you like.
date($format, $timestamp);
http://php.net/manual/en/function.date.php