简体   繁体   English

如何使用CodeIgniter在URL中传递MySQL Datetime值?

[英]How to Pass MySQL Datetime value in URL using CodeIgniter?

I would like to pass a MySQL Datetime value like below in a site URL which is build using Codeigniter. 我想在使用Codeigniter构建的站点URL中传递如下所示的MySQL Datetime值。 So the datetime value is eg 所以datetime的值例如

2012-05-04 07:41:45

And I want it to be passed in a site URL using some king of encoding or string conversion. 我希望使用某种编码或字符串转换之王将其传递到网站URL中。 As I have already tried using php urlencode() function but it is not working. 正如我已经尝试使用php urlencode()函数,但是它不起作用。

The reason to pass this in URL is because I am querying some records from database related to that Datetime. 在URL中传递它的原因是因为我正在查询数据库中与该Datetime相关的一些记录。 So I must pass it as an Argument to Controller and for that I must pass it under URL. 因此,我必须将其作为参数传递给Controller,并且为此必须在URL下传递。

Any suggestion? 有什么建议吗?

Thanks in advance. 提前致谢。

Easy: 简单:

function receiving_function($time)
{
     $date = date("Y-m-d h:i:s", $time);  // Format this however you want the data
}


function sending_data()
{
    $date = strtotime("2012-05-04 07:41:45");
    redirect ('/yourcontroller/receiving_function/'.$date).
}

If your datetime is inside timestamp boundaries, you could convert it to a Unix Timestamp: 如果日期时间在时间​​戳范围内,则可以将其转换为Unix时间戳:

function mysql2timestamp($datetime){
   $val = explode(" ",$datetime);
   $date = explode("-",$val[0]);
   $time = explode(":",$val[1]);
   return mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);
}

just use strtotime 只需使用strtotime

echo strtotime('2012-05-04 07:41:45');

Outputs: 输出:

1336088505

You could also base64_encode it to preserve the exact string, but I doubt that's really worth it ;-) 您还可以对它进行base64_encode以保留确切的字符串,但是我怀疑这是否真的很有价值;-)

To preserve the exact string, I sometimes convert any non-standard url query values to hexadecimal from ASCII. 为了保留确切的字符串,有时我会将所有非标准的url查询值从ASCII转换为十六进制。 On the next page, convert from hexadecimal to ASCII. 在下一页上,从十六进制转换为ASCII。

Refer to http://www.pgregg.com/projects/php/code/hexstr.phps 请参阅http://www.pgregg.com/projects/php/code/hexstr.phps

In some situations, I find it useful when passing search query keywords as well. 在某些情况下,当传递搜索查询关键字时,它也很有用。

"There's an app for that!" “有一个用于此的应用程序!”

Codeigniter provides a handy encryption class - no main improvements over base64 but the benefit is that you get to choose the cipher you want. Codeigniter提供了一个方便的加密类-与base64相比没有重大改进,但是好处是您可以选择所需的密码。

Instead of passing into the URI, could you not collect from $_GET? 除了传递到URI之外,您还不能从$ _GET收集信息吗?

$this->load->library('encrypt');

$datetime = "2012-05-04 07:41:45";

$encrypted_string = $this->encrypt->encode($datetime);

$hello_again = $this->encrypt->decode($encrypted_string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM