How to display datetime form mysql using CakePHP 3.0 - mysql

I would like to ask how to add datetime ('Y-m-d h:i:s') format field for the SQL.
The name of my table is groups, the name of the field is date_add
i am using cake PHP 3, i want to use the timezone Australia/Perth but i don't know how to begin.
I successfully displayed added my date using
$group->date_add = date("Y-m-d h:i:s");
However, the result for the time is not correct with the timezone.

Datetime in PHP in general
Per the documentation of DateTime you can use all options available for the date() functions for formatting. Your main mistake in the formatting is the difference betweetn a y and a Y being the difference between a year in two and a year in four numbers.
Secondly you say you want to add the correct time zone. This is a bit odd however since you always want to add the same time zone. If you want to convey the fact that all your dates are Australia/Perth as information why do you not simply add that text after it?
If you mean this is a problem since you store the information in a different time zone to begin with and thus have a conversion problem you can set the correct time zone on the DateTime object itself. But you need to be sure the DateTime object is constructed with the correct original time zone to begin with. Observe the following code for an explanation:
<?php
$DateTime = new DateTime(); // This is now Europe/Amsterdam for my laptop
var_dump($DateTime->format('dmY h:i e'));
// result of var_dump is: string(31) "13102015 12:00 Europe/Amsterdam"
$DateTime->setTimeZone(new DateTimeZone('Europe/London'));
var_dump($DateTime->format('dmY h:i e'));
// result of var_dump is: string(28) "13102015 11:00 Europe/London"
Take aways:
e is the format modifier for the time zone
Conversion of time zones is possible with PHP's DateTime object. Find out what your default time zone is on your current PHP installation to see if you need to convert or not. See information on the date.timezone setting here: http://php.net/manual/en/datetime.configuration.php
Cake 3.0 specific
As Oops D'oh pointed out in the comments there are a lot of CakePHP specific things to know as well. Since he added an excellent part concerning that I suggest you read that as well.

Related

Laravel + Carbon + SQL time formatting problem

Can someone please assist me with the following problem. I am using Carbon to get the current time. Once I got the time and send it to my (myphpadmin) database it displays the whole date and time and not just the time. Here are all the code being used.
Laravel Code:
$date = Carbon::parse(now())->timezone('GMT+2');
$time = $date->toTimeString();
$UserRequest->finished_at = $time;
SQL Database layout and format:
Display: (Incorrect)
I have literally tried all the custom formatting from Carbon docs nothing sends over just the time.
I need this format -> 12:09 pm
Table Structure:
The problem more complicated than you describe.
First looks as finished_at is datetime field, so you can not store only date in this field. Sure, you can change the column format to time and store only time part. But this approach can cause problem with overdate date (started_at may be previous day or early).
So I think you need not change your storing flow, but you can change representation flow by using appropriate format

How to use Time Data type only in Rails?

I use time as datatype in my rails migration but when I am retrieving it in model
it includes date. I wanted to be able to compute time difference without date.
Any way I can do to make my model return time only ?
I wanted this 03:15:00 not this 2000-01-01 05:54:42
If you want to convert a Datetime object into a Time object, you can use Time.parse.
date_and_time = Time.now # 2000-01-01 05:54:42
time_only = Time.parse(date_and_time) # 05:54:42
If you use the time data type for a column in your database migration you should be fine. Rails fills the missing fields (in this case year, month, day) with default values since the data type requires them. Since all responses get these default values you should be able to calculate time differences without date since all values have the same default value. If you want to get the time as string back in the format you want you can use #strftime method.
Eg:
time.strftime('%H:%M:%S')

How to separate time from datetimepicker vb.net

I have one datetimepicker which custom format MM/dd/yyyy h:mm tt
I have database and has a column "Date_Time" the value of the DateTimePicker is saved to the column Date_Time formatted like this MM/dd/yyyy h:mm tt
now i want to get the Time only not the entire value of datetimepicker just the hh:mm tt from the column Date_Time
SORRY FOR MY GRAMMAR
How about DateTime.TimeOfDay?
It returns the time that has elapsed since midnight (which is what h:mm tt stands for in your code).
Dim Time As TimeSpan = DateTimePicker1.Value.TimeOfDay 'Would return for example 3:14 PM
The answer above is right.
If you need to get time string, you can use also another way, which includes a formating:
Dim myTimeString = DateTimePicker1.value.ToString("hh:mm")
You can do that for any part of the DateTime value.
You are heading for a new problem. If you zero out the Date portion and store the result to a DateTime column, you will end up storing something like: 0001-01-01 16:43:12. A column defined as DateTime will always have a Date, as will a DateTime variable.
The first problem may be getting MySQL to accept a non-Date in a DateTime column. Using a column defined as DateTime(3), mine throws a generic fatal error exception trying to store just a TimeSpan to it:
cmd.Parameters.Add("#p3", MySqlDbType.DateTime).Value = DateTime.Now.TimeOfDay
cmd.ExecuteNonQuery()
If MySqlDbType.Time is used as the type, I get an exception that the time is an invalid value for the column...and it is.
If you manage to store it somehow, the next problem will be when/if you want to put that value back in a DateTimePicker: the minimum date you can enter is 1/1/1753 (first full year of the current calendar). So your DateTime var with the Date zeroed out wont work. You'll first have to restore the date portion, but the Date, Year etc are all readonly.
Solution 1
Define the column as Time(0) which will store hours, minutes and seconds. Use the value in the parens to specify fractional seconds, for instance Time(3) will also store milliseconds. When you read the data, store it to a TimeSpan.
Then in your UI use a different control, otherwise you have the same problem - adding some Date data to it to make it usable in a DateTimePicker
Solution 2
Use a DateTimePicker and a DateTime column, but just ignore the Date portion in your code. This will allow you to use what is in the Database as is with the control.
You can get the time selected with DateTime.TimeOfDay but storing and reusing it may be problematic.

How do i stop Entity Framework converting Json UTC datetime when i want to store UTC datetime

Banging my head against the wall on this issue.
I receive a text string to my web service which is generated with json, for example:
"2014-09-19T17:00:00.000Z"
When i assign this to my database in entity framework, it changes it to 20:00 instead of 17:00. So i specify that it is UTC date by
DateTime.SpecifyKind("2014-09-19T17:00:00.000Z", DateTimeKind.UTC)
But this returns 20:00 as well! I could fix this by removing the Z but that is a dirty workaround as could be receiving time zones instead of Z. Is there way of setting telling vb to ignore the timezone and just save it as 17:00?
As described here EF will always read the date time value as if it was of Unspecified kind since DateTime does not store time zone. Consider using DateTimeOffset which does store time zone.
Thanks to Pawel for the hint. To help anyone else with this problem, please see the code below:
New DateTimeOffset("2014-09-19T17:00:00.000Z").ToUniversalTime

Last Active List - VB.NET

I've got a client application that's going to update a database every five minutes with the current time, and then I want to output this time as a last active table in a seperate VB application.
I know about mysql time, but I don't quite understand how I can use it to display when a client was last active.
I've looked around and found some stuff about mysql times but I don't fully understand it.
Any help would be great, I'm going to place the results in a ListView with 'Client Name' and 'Last Active' if this helps, and I already know how to connect to my database and retrieve information.
Thank you.
I'd recommend using a DATETIME for storage. The TIME data type is limited to a single "time of day" or a timespan. True, you're looking for the time of day, but to calculate the "Last Active" time you need the date attached. Consider these "Last Active" values (using a 24-hour clock):
3/26/2013 at 17:00:00 <-- this has the maximum time (5PM), but...
3/27/2013 at 08:15:00 <-- ...this is the most recent time because it happens the following day
In other words, you need the date so you can sort the time.
The MySQL DATETIME data type should be supported by VB.NET, but I've never used the two together so I can't guarantee it. To query and report just the time component of the date you have a ton of options. Here are two:
Query the entire date/time from MySQL and return it as a System.DateTime value to VB.NET. In VB.NET you can format it using DateTime.ToString to show only the time components. The MySQL query would go something like this:
SELECT ClientName, MAX(LastActive) AS LastActiveDateTime
FROM your_table
GROUP BY ClientName
Format the time in MySQL and return it as a String to VB.NET. In VB.NET you'll just need to display the string as is. The MySQL query would go something like this:
SELECT ClientName, DATE_FORMAT(MAX(LastActive), '%r') AS LastActiveTime
FROM your_table
GROUP BY ClientName
The format code %r in the above query will return the time in a 12-hour format with AM/PM, for example 07:55:29 PM. To return a 24-hour format (19:55:29), use %T instead.