Laravel 4.1 - display time format from mysql column TIME - mysql

I have a column in mysql database named time_start which type is TIME.
I am trying to format the displayed time in my view from H:i:s to simple H:i, so instead of 20:00:00 i will get 20:00.
Firstly i thought i can use format() method, but it's supposed to be used with timestamps, so of course i get error Call to a member function format() on a non-object.
I'm sure it's a simple question, but i can't solve this.
-EDIT-
Forgt to mention. I am working with a many to many relationship and i'm calling my data like:
$schedule->time_start
Here i would like to show the time in H:i format, not H:i:s.

Have you tried an accessor method? I.e. in the Schedule model,
/**
* Return a truncated version of the timestamp.
* #param $value original value of attribute
* #return string truncated value
*/
public function getTimeStartAttribute($value)
{
// Input is HH:MM:SS
return substr($value, 0, 5);
// Output is HH:MM
}
Then $schedule->time_start ought to return the time in HH:MM format.

Related

Doctrine2 converts date type value to SQL format in wrong way

I have not yet understood where concretely is the problem is...
I have a Doctrine entity with date field:
/**
* #var \DateTime
*
* #ORM\Column(name="day", type="date")
*/
private $day;
Also I have an entity form with date type field:
$builder->add('day', DateType::class, ['widget' => 'single_text'])
And when I try to save the form with value "2016-02-14" I see that it becomes "2016-02-13" (a day earlier) in MySQL and in PHP after saving. When I began looking for logs, I saw that the query parameter value is "2016-02-13 23:00:00".
But I don't understand why it happens this way.
I have the same time and timezone in system (Ubuntu), PHP and MySQL (Europe/Moscow timezone).
I use date type, not datetime, so there should not be time at all.
When I tried to debug it, I saw that code
#vendor/doctrine/dbal/lib/Doctrine/DBAL/Types/DateType.php
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return ($value !== null)
? $value->format($platform->getDateFormatString()) : null;
}
works correctly. It makes correct format "Y-m-d", but in the symfony log value is with time.
I need an advice about how to find, where my date transforms.
I had a similar problem and used a quick-and-dirty hack. On the setter used for the $day property, just set the time to be midday.
public function setDay(\DateTime $day)
{
$this->day = $day;
$this->day->setTime(12, 0, 0);
return $this;
}

Use the same buffer for separate DATE and TIME

I am using the MySQL C api along with prepared statements. When issuing a INSERT query with a TIME field the result is in HHH:MM:SS format, where the hours are the amount of hours elapsed this month. For instance if the date is 2015-02-21, and the time is 21:30:00 the time would be displayed as 525:30:00 but I want to use the HH:MM:SS format instead (e.g 21:30:00), which would be the actual time of the day.
sbind[3].buffer_type=MYSQL_TYPE_DATE;
sbind[3].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[3].is_null= 0;
sbind[3].length= 0;
sbind[4] = sbind[3];
sbind[4].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
This code will prepare the date field as yyyy-mm-dd and fill it with the date in tm_info. Likewise it will do the same thing for the time field but in the HHH:MM:SS format.
A unfashionable way which works is to use a separate MYSQL_TIME structure for the time, but I aim for a more elegant way to handle this.
(EDIT: Here I have included the relevant client side code
MYSQL_TIME ts;
MYSQL_STMT *stmt;
MYSQL_BIND sbind[2];
...
char query[QUERY_BUFFER_SIZE];
strcpy(query, "INSERT INTO `mytable` (date,time) VALUES(?,?)");
if(mysql_stmt_prepare(stmt, query, strlen(query))){
return mysql_stmt_errno(stmt);
}
...
time_t rawtime;
time(&rawtime); // get current time
struct tm *tm_info = localtime ( &rawtime );
...
memset(sbind,0,sizeof(sbind));
sbind[0].buffer_type=MYSQL_TYPE_DATE;
sbind[0].buffer= (char *)&ts; // Pointer to a MYSQL_TIME data structure
sbind[0].is_null= 0;
sbind[0].length= 0;
sbind[1] = sbind[0];
sbind[1].buffer_type=MYSQL_TYPE_TIME;
mysql_stmt_bind_param(stmt, sbind); // sbind is an array of MYSQL_BIND structures
ts.year= 1900+tm_info->tm_year; // tm_info is a pointer to a tm structure
ts.month= 1+tm_info->tm_mon;
ts.day= tm_info->tm_mday;
ts.hour= tm_info->tm_hour;
ts.minute= tm_info->tm_min;
ts.second= tm_info->tm_sec;
if(mysql_stmt_execute(stmt)){
return mysql_stmt_errno(stmt);
}
This assumes mysql is a valid connection. The table mytable in this case only contains a DATE type and a TIME type.
)
That's the natural representation for the TIME type - it, as the name suggests, only holds time, and this is the natural way to express time larger than a day. It does, as the documentation suggests, use HH:MM:SS for smaller values.
Since TIME does not care about shenanigans like leap seconds, to exclude full days, just take tm_hour%24. But, to allow for shenanigans like DST transitions, you have nothing to do but add the TIME to the starting point of the specific month and do DATETIME arithmetic with the stock functions.
#c45602234:
Trying to outsmart libmysql FAILED (mysql-connector-c-6.1.5/libmysql/libmysql.c:1964):
static void store_param_date(NET *net, MYSQL_BIND *param)
{
MYSQL_TIME tm= *((MYSQL_TIME *) param->buffer);
tm.hour= tm.minute= tm.second= tm.second_part= 0;
net_store_datetime(net, &tm);
}
As you can see, it always uses up the entire structure (apparently, so that relevant functions always work as expected).

to change the date format in data base

it is not able to store day,month,year in the format of dd/mm/yy in data base by getting value through the form of Yii.it is only possible to store in the format of year/month/date can any body answer.
Your question is not clear. I am guessing when you save date submitting the form it saves in mysql default format and then when you display it, it is not displayed in your desired format. If this is the case, I would recommend the best way to solve this is to change the date format in the model. You can use the 'before save' and 'after find' methods in the model to change the date format before saving to db and also change it when it is pulled from db. The actual date format will remain the same in db, but it will also display in the format you want it to. Here's an example,
protected function beforeSave()
{
if($this->date!=null){
$this->date=date('Y-m-d', strtotime($this->date));
}
return TRUE;
}
public function afterFind()
{
if($this->date!=null){
$this->date = date('d F Y',strtotime($this->date));
}
return true;
}

how to save object with sysdate functionality

i have an object student. then there is a property called expiry date. this is need to be set with the database sysdate + a value(1000).
so how can i save with jpa. can't i do it on the jpa prepared statement query itself?
if i use sql.date is it exactly give the same value as when we are saving as 'sysdate'?
can't i do it with on the query itself?
other properties can be set to the object. but the problem is this expiry date as it needs the sysdate and add another value to it eg: expiry date = sysdate + 1000; how can i do it with jpa prepared statements. please reply me
What about use a seperate query to retrieve sysdate and set it to your object.
I usally create a Clock to handle this:
public interface Clock {
Date now();
}
public class HibernateClock implements Clock {
//use query to retieve the db sysdate
}
You can add it in java itself. Use calendar object to add days.
Calendar expirydate=Calendar.getInstance();
expirydate.add(Calendar.DATE, 1000);
then
expirydate.getTime() will give you expire date object.
Why do you want to use sysdate? Its syntax is database specific and also dependent on the DB-hosting machine's clock, rather than on your application-hosting machine's clock.
Easiest way is to use java.util.Date as the expiryDate's type and the value of new Date(System.currentTimeInMillis() + 1000). Use this value in the field's declaration for featuring it as default value on new Student creation or use it as the value passed to the setter when modifying an existant Student.
public class Student {
...
/**
* Using java.util.Date here. Hibernate knows to convert it automagically to java.sql.Date.
* Set default value to current time + 1 second, if this is your requirement.
*/
private Date expiryDate = new Date(System.currentTimeInMillis() + 1000);
public void setExpiryDate(final Date expiryDate) {
this.expiryDate = expiryDate;
}
...
}

DateField: selectedDate shows hours as 24:00:00

With a DateField component, the selectedDate.getHours returns as 24:00:00. I want it to return as 00:00:00.
Is there an easy way to do this?
Thanks!
UPDATE:
First, I set a variable in my Model that equals the selectedDate of a DateField component:
model.generalInfo.endDate = endDate_df.selectedDate;
Then I set another variable based on that value and I trace it out:
param.todate = df.format( model.generalInfo.endDate.toString() );
And this is where I see the time equal to 24:00:00
you could try something like
selectedDate.time = selectedDate.time - 24 * 60 * 60 * 60 * 1000
as a Date.time represents miliseconds since 1970 or whatever.. you substract 24 hours..
if it not works for you, you can create a new function or getter that converts it, or you can create a new mxml module, with DateField as superclass, and you can override the getHours method. tons of options to do this..
It looks like you are using the Flex DateFormatter to format the Date object. Have a look at the docs for this class, it has a formatString property that you can use to control how to output the date (or in this case the time).
If you give the DateFormatter a format string that contains "H" will output the hour in 24 hour format using the number range 1-24. If the format string contains "J" it will output the hour in 24 hour format using the range 0-23.
To get your desired output, use "JJ" in the format string, in addition to any other items. For example to output the hours, minutes, seconds:
var someDate:Date = new Date(2012, 11, 5);
var df:DateFormatter = new DateFormatter();
df.formatString = "JJ:NN:SS";
var formatted:String = df.format(someDate); // 00:00:00
Also, as #Flextras mentioned, there is Flash localization API you can use which has the added benefit of converting date/time strings to the values used by their locale. These are the DateTimeFormatter classes:
fl.globalization.DateTimeFormatter
spark.formatters.DateTimeFormatter (Flex 4)
These classes format dates into the user's locale (or one that you specifcy), and format Date objects the same way the DateFormatter does.