Welcome Guest, Not a member yet? Register   Sign In
need the number of seconds since....
#1

(This post was last modified: 08-14-2018, 11:55 AM by richb201.)

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
Reply
#2

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'); 
Reply
#3

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
Reply
#4

(This post was last modified: 08-14-2018, 10:25 PM by richb201.)

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
Reply
#5

(This post was last modified: 08-15-2018, 12:17 AM by Pertti.)

You could try strtotime function, it's pretty good figuring out what's going on.
Reply
#6

(This post was last modified: 08-15-2018, 12:38 AM by jreklund.)

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
defined
('BASEPATH') OR exit('No direct script access allowed');

if ( ! 
function_exists('UUID_TO_BIN'))
{
    function 
UUID_TO_BIN($uuid) {
        
$uuid hex2bin(str_replace('-','',$uuid));
        return 
$uuid;
    }
}

if ( ! 
function_exists('BIN_TO_UUID'))
{
    function 
BIN_TO_UUID($uuid) {
        if( empty(
$uuid) ) return;
        
$uuid bin2hex($uuid);
        
$uuid substr($uuid08) . '-' substr($uuid84) . '-' substr($uuid124) . '-' substr($uuid164)  . '-' substr($uuid20);
        return 
$uuid;
    }
}


if ( ! 
function_exists('UUIDv4'))
{
    
/**
     * Generates a random UUID using the secure RNG.
     *
     * Returns Version 4 UUID format: xxxxxxxx-xxxx-4xxx-Yxxx-xxxxxxxxxxxx where x is
     * any random hex digit and Y is a random choice from 8, 9, a, or b.
     *
     * @return string the UUID
     */
    
function UUIDv4()
    {
        
$bytes random_bytes(16);
        
$bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
        
$bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
        
$uuid vsprintf('%s%s-%s-%s-%s-%s%s%s'str_split(bin2hex($bytes), 4));
        return 
$uuid;
    }
}

if ( ! 
function_exists('VALID_UUIDv4'))
{
    function 
VALID_UUIDv4($uuid)
    {
        return 
preg_match('/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i',$uuid);
    }


PHP Code:
$date = new DateTime();
echo 
$date->format("Y-m-d\TH:i:s\Z");
// 2018-08-14T23:53:57Z

$date = new DateTime();
echo 
$date->format("Y-m-d\TH:i:s\Z");
// 20180814T235439Z 

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");
$gregorian->setTime(00);                        
echo 
gregoriantojd$gregorian->format('n'), $gregorian->format('j'), $gregorian->format('Y') ); 
Reply
#7

(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
Reply
#8

You can use substr to cut out first 4 characters substr($avtivity_date, 4)
Reply
#9

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 )
Reply
#10

(This post was last modified: 08-15-2018, 05:59 AM by richb201.)

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB