need the number of seconds since.... |
I have some date for example July 9, 2018 that I am keeping in my dynamodb table. But I actually have two dates for that day, July 9 2018 AM and July 9 2018 PM. I need to convert each of those into a different number. They can't be the exact same. I thought about using time(). What would be perfect if I could somehow generate a date like July 9, 2018 0800 (which is 8AM) and July 9 2018 1400 (which is 2PM) and then convert each date-time into a number of seconds since the epoch.
The reason to do this is that I am using dynamodb and I want to get all the records between Jan 1st 2018 and Dec 31 2018. But as i said i am not allowed to have two records with exactly the same value, and I don't think it can compare dates, just numbers or strings. Any ideas?
proof that an old dog can learn new tricks
AWS recommends storing dates in ISO 8601:
https://docs.aws.amazon.com/amazondynamo....DataTypes http://en.wikipedia.org/wiki/ISO_8601 2016-02-15 2015-12-21T17:42:34Z 20150311T122706Z http://php.net/manual/en/class.datetime.php http://php.net/manual/en/datetime.createfromformat.php PHP Code: echo DateTime::createFromFormat(DateTime::ATOM,'2015-12-21T17:42:34Z')->format('U');
So my current activity dates appear like this: "Mon Aug 13 2018". I am not sure what the T and Z and the format('U') is about. Stepping with the debugger I do see that your date '2015-12-21T17:42:34Z' was transformed to a number. so that is looking good. How would I create that string in PHP. In C I would used sprintf to output to a buffer. Does CI/PHP have something analogous?
20150311T122706Z
proof that an old dog can learn new tricks
Well, I figure I will use uniqid() to create a unique field (instead of auto increment like mysql) for use in Dyanamodb. That still leaves one small problem.
My date fields appear like this Thu Aug 16 2018. I realize that this is not one of the Supported Formats in standard_date(). This field is coming from a Jquery control on a client browser. So there is little I can do to modify the format. How can I change Thu Aug 16 2018 to a julian date in PHP/CI?
proof that an old dog can learn new tricks
T = Time
Z = Zone So that parsers know where to start looking. U = Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) This is the same as time(), like you wanted. Personally I'm using UUIDv4 instead of auto incremented. And storing them as BINARY(16). But you can store them as strings too. PHP Code: <?php PHP Code: $date = new DateTime(); YOU NEED TO INSTALL CALENDAR TO USE THIS FUNCTION http://php.net/manual/en/book.calendar.php http://php.net/manual/en/datetime.settime.php http://php.net/manual/en/function.gregoriantojd.php This assumes that days are counted like this: 1, 2, 3, 4 ... NOT 01, 02, 03, 04 ... PHP Code: $gregorian = DateTime::createFromFormat("D M j Y","Thu Aug 16 2018"); (08-15-2018, 12:16 AM)Pertti Wrote: You could try strtotime function, it's pretty good figuring out what's going on. I tried it on a date Thu Aug 16 2018. It didn't like the Thu and returned false. I then tried strtotime($activity_date[4]) thinking I could cut out "Thu ". But that came up with false too. Sorry, I think in C. How can I cut off the day from the string?
proof that an old dog can learn new tricks
If you need seconds you can use the MySQL DATETIME and TIMESTAMP fields to get seconds also.
TIMESTAMP(4) and DATETIME(4) The 4 can be change for how many seconds you need. SEE: MySQL - 11.3.6 Fractional Seconds in Time Values What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks Insite. I am not using mySQL for this part of the code. Instead I am using dynamoDB. I am concerned about something though. Right now I am using uniqid(). I am sometimes writing out 10 record at a time in rapid sequence. Besides the fact that for right now control doesn't come back to the user for say 10 seconds, I am concerned that two records will get the same uniqid.
Right now I am on my localhost testing the CI code but with the live dynamoDB. So there is the delay. But this thing will be hosted on Amazon Lightsail which I think will probably have much higher transfer speeds to dynamoDB. if uniqid() is based on number of seconds, that might mean two records will have the id and thus the second will fail to be written. For right now, with the delay due to upload speed, all is AOK. Thx.
proof that an old dog can learn new tricks
|
Welcome Guest, Not a member yet? Register Sign In |