Hi I have posted this question before, but people marked it as a duplicate questions. so I didn't get any answer.
I am trying to get the current date/time in the format for example "20150701183741.941Z', I am not sure what the technical term for this format is called. some people told me that it is UTC/ZULU time format.
I have tried generate the above format by using below codes.
date_default_timezone_set("UTC");
echo "formatted currenttime: ".date("YmdHis.ue", time());
but it returns it in the format "20150701144710.000000UTC", this is close to what I need, but I am not able to get at the end something similar to ".941Z".
Please note that this isn't a duplicate question. I posted it earlier but got marked as a duplicate and that one is now inactive. Thanks very much for your help.
The official documentation of date() says that date() always generates 000000 because it takes an integer parameter, and that DateTime() does support microseconds:
http://php.net/manual/en/function.date.php
However, this comment seems to say that DateTime() doesn't support microseconds either:
http://php.net/manual/en/class.datetime.php#108970
So I guess you're out of luck with that :(
Why don't you use the Carbon Library? It's one of the superb ways to handle data and time. Just take a look into the official documentation at http://carbon.nesbot.com/docs/
I'm trying to use
format("m/d/y \a\\t h:ia T");
to get
05/14/15 at 04:22am MDT
which used to work but now all of a sudden I'm getting
Exception: DateTime::__construct() [datetime.--construct]: Failed to parse time string (05/14/15 at 04:22am MDT) at position 9 (a): The timezone could not be found in the database in some/folders/and.files on line x
So my work around for now is taking the "at" out
format('m/d/y h:ia T');
I've found I can leave the \a in with no problem but the \a\\t causes the error. Any on how to get past this?
**edit
The timestamp being passed in is generated by mysql
2015-05-14 10:22:41
**edit
This accepted answer didn't actually solve the problem for me. I am certain the issue is unique to my server / application so once I figure it out on my own I'll post back here as to avoid any further downvotes.
I'm working with a LOT of data and a LOT of files (classes) to get this thing working so I couldn't paste all my code in here or else no one would look at this question. So here's the skinny on what was actually happening:
Inside of a loop my time value was getting replaced with the formatted value and then getting caught up in the same loop, hence why I was getting that error and everything thought I was getting time with an incorrect value because ultimately I was (the first time it was okay but not on the re-run).
resolution
When in doubt, print out the time value going INTO your DateTime->format and you may be surprised that it's not what you think it is.
Timezones and timestamps confuses me so I'm hoping someone can answer my questions :)
Lets say I have a Python script that parses an RSS feed, converts the date value into a timestamp using the following code and stores it in a database:
article_date = parse(article.published).strftime('%s') if hasattr(article, 'published') else round(time.time())
Now when I retrieve that record from the db in PHP, and I run the following code, does PHP assume the timestamp was UTC-0 and automatically offsets the timezone to Eastern time?
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s',$timestamp);
I'm seeing weird issues with my dates, so I'm wondering if someone can help me out with advice on how to properly convert and store rss feed timestamps. I can across this line of code somewhere so should I put this at the top of my script?
os.environ['TZ'] = 'Europe/London'
If you want to set your timezones and keep them aligned in PHP and in Python, then your PHP code is completely correct and for python you need to apply the following:
os.environ['TZ'] = 'America/New_York'
time.tzset()
before you call strftime()
That should make sure you store the time in the same zone you're trying to retrieve it.
Note: tzset() is a Unix-only function.
I've got a pretty simple question. I send a date as millis created by javascript to a php site and use phps getdate to get information about the date in an associative array. My timezone is GMT+2, but I do not understand why date conversion using milliseconds should have any effect like this. Can someone please explain this to me?
Thanks!
It sounds like PHP doesn't know what the timezone is supposed to be or the system timezone is not properly set. The simplest fix is to set the timezone you want yourself in the PHP code.
I am having an add/edit form to update and add to database, and I was not sure what the best way is to input TIME type (HH:MM:SS). Should I use multiple html text inputs for HH, MM, SS?
if so, is there a function that prepares the string for database input?
Basically what I'm trying to input is how many hours, minutes, seconds a specific task took to finish.
Can anyone point me in the right direction here?
I'm designing a website using Codeigniter (PHP).
Thanks
Let me go ahead and clarify what needs to happen a bit more...
The user is required to enter data specific to sports more in particular to a players minutes and seconds played. I'm thinking of maybe simplifying it to only minutes. Perhaps this way input is only 1 thing. Then again my question is, what method would work to convert this "minute number" to the correct MYSQL TIME format?
I wrote a helper to do something similar in an app I'm working on. Mine generates three dropdowns, hh, mm and am/pm, by calling built in form_dropdown helper. Once I get the data from the drop downs, I convert it 24hr format and then I just concatenate the strings into the right format for MySQL. Since it's a helper I can just call it from any view using form_time(). I can post it here if you think it would help to see it.
Dana
just use now() function, if your DB is
MYSQL is you want to save the current
time
e.g:
UPDATE tbl SET timemodified = NOW()
and make sure that timemodified has a
type of "time"
Ow sory miss that point. uhm maybe you need to have a start time in your DB, then after he/.she is finish his/her task. you must query on the DB the start time subtract it to the end time(your current time) then the result would be the time he/she performed the task
this checks for 2 numbers, then a ":" then 2 numbers, then a ":", then finally 2 numbers again:
$cleanTime = preg_match( '/(\d\d)\:(\d\d)\:(\d\d)/', $_POST[ 'NAME_OF_TIME_INPUT' ] );
if( !$cleanTime ){ /* ... error ... */ }
don't be scared of all the slashes, haha (I was at first when I used regexps).
replace NAME_OF_TIME_INPUT with the content of the name attribute on the <input on the <form page
i.e. if <input name="coolinput" /> then use $_POST[ 'coolinput' ]
This is one of the eternal struggles of (web) UI design, how to input time without driving the users nuts. What works for your specific case is something only you can decide, because it depends on the exact format/circumstances you need and your target audience.
As general guidelines I'd say:
Don't do a free-form text field that requires a certain format, e.g. "Enter time (HH:MM:SS)", because it's too easy to mess up and will deny the users input or mess up the time if you do no validation.
Try to avoid [0-23] [0-59] [0-59] dropdowns, since they can be quite a pain (click, scroll, click, click, scroll, click, click, scroll, click).
If ease of use is a high priority, as would be the case for public websites, maybe a Javascript enhanced timepicker is a good idea. Try not to use anything too fancy that nobody gets though (like dragging the hands on a clock).
A free-form, free-format text field might be the best idea. The user can just type in "3pm", "16:34" or "midnight". You may need to provide examples to get users started, otherwise they may feel lost. You can run this through strtotime on your end, but you may need to fill in the blanks and do a lot of validation.
Three short text fields may be a good idea if your audience is very keyboard focused and can be expected to tab through them in rapid order.
As for formatting it for SQL, however you receive the time input from the user, you should assemble it to a UNIX timestamp and format that timestamp for SQL:
date('Y-m-d H:i:s', $timestamp);
I have searched for some alternatives and solutions and I came up with this:
$min = 60;
$time[] = floor($min/60);
$time[] = $min%60;
And I used the following to convert to MySQL TIME format
INSERT INTO table (min) VALUES (MAKETIME($time[0], $time[1], 0))
I have had the same issue with working with the html time input. However, I've managed to work around it with a PHP function.
What the function does is translate the time into a format the MYSQL DATETIME datatype can understand.
Of course you will need to enter the date somehow, but I'll leave that up to you.
functions.php
function convertHtmlTime($date,$time){
$newDate = date($date);
$newTime = date($time);
$datetime = new DateTime($newDate.$newTime);
return date_format($datetime, 'YmdHis');
}
test.php
$date="2007-02-16";
$time="23:59";
echo convertHtmlTime($date,$time);
Results:
20070216235900