Zend_Date strange probleb - php

I use
<?php
$date = '2014-09-01 14:01:52';
$dateObject = new Zend_Date($date);
$tranDate = $dateObject->get(Zend_Date::ATOM);
echo $tranDate;
?>
2014-09-01T14:01:52+00:00
it's correct, but i noticed than several times it returned
2014-01-09T14:01:52+00:00
Does anybody know what's the problem?

The problem may be related to your $locale.
You should be fine doing this
$dateObject = new Zend_Date($date, 'dd/MM/yyyy');
Or, if you want to be "cleaner", instead of writing the string, check out Zend constants for dates here
http://framework.zend.com/manual/1.12/en/zend.date.constants.html

Related

Random String Generator that does not repeat in laravel controller

I created this function in my laravel controller.
function incrementalHash($len = 5){
$charset = "0123456789abcdefghijklmnopqrstuvwxyz";
$base = strlen($charset);
$result = '';
$now = explode(' ', microtime())[1];
while ($now >= $base){
$i = $now % $base;
$result = $charset[$i] . $result;
$now /= $base;
}
return substr($result, -5);
}
then I have a function to insert something in the database. this function uses the above function. but every time I use it I get the same result from above function. I tried composer dump-autoload and the result changes. I wonder what is happening? why this method always returns the same result. how can I use this method and not receive the same result without dumping autoload? here is my controller:
public function add_user_create()
{
$user = new User;
$user->user_id = Request()->input('user_id');
$user->user_name = Request()->input('user_name');
$user->fcm = Request()->input('fcm');
$user->email = Request()->input('email');
$user->token = Request()->input('token');
$user->profile_pic = Request()->input('profile_pic');
$user->api_token = str_random(60);
$user->ref_ID = $this->incrementalHash(4);
$user->save();
}
I suggest you to use what Laravel provides to generate a random string. like: strtolower(str_random(4)) as mentioned by #kenken9999
However, Here is why I think it gave same result for you:
I executed your function multiple times and these are the outputs:
becpy
becqa
becqd
becqd
becqe
I think when you checked them they just happend to be same and when you did composer dump-autoload you happened to see a different output.
Let me know If I am wrong.
Did you call this function many times during a very short time? Then I believe the issue is microtime(). This function returns a string separated by a space. The first part is the fractional part of seconds, the second part is the integral part.
Thus, if the function is called during the same second, $now should be the same, based on which $result will not change.
Further, if the function is called during a short time (let's say several seconds), $now would be similar (1283846202 and 1283846203 for example). In this case, only the right part of $result would vary.

Same variable type (String) but not work when use

10-01-2014 ===> means 10 jan 2014
i have source code like this:
foreach($report_data['summary'] as $key=>$row) {
$substrdate=substr($row['payment_type'],-16); //i have check the result is 10-01-2014 & 11-01-2014
$stringvar = '10-01-2014';
$date = DateTime::createFromFormat('d-m-Y', $stringvar);
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$date->format('Y-m-d').'</span>', 'align'=>'right');
$summary_data_row[] = array('data'=>'<span style="color:'.$color.'">'.$row['comment'].'</span>', 'align'=>'right');
}//end of foreach
it runs well '10-01-2014' become '2014-01-10' in string type as what i want ..until i substitute variable $stringvar with $substrdate which has same value -> '10-01-2014' , syntax $date->format('Y-m-d') makes my program blank page. no error shown in logs.
pls help
I also faced the same problem. and my solution was the following code.
$stringvar = '12-01-2014';
$date= date('Y-m-d',strtotime(str_replace("-","/",$stringvar)));
Try this
//$stringvar = '10-01-2014';
//$date = DateTime::createFromFormat('d-m-Y', $stringvar);
Use this instead of above one
$stringvar = '10-01-2014';
$date = date("Y-m-d", strtotime($stringvar));

PHP format date

How can I force the date format to output:
12/12/2012, 1/10/2012, 1/5/2012
instead of
12/12/2012, 01/10/2012, 01/05/2012?
My code is the following:
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'd/m/Y');
$adateto = date_format($adateto, 'd/m/Y');
Please do note that I have to format the date AFTER posting it.
Have a look at the PHP built in date function here
You will find that your solution is as simple as this:
date('j/n/Y',strtotime($_POST['datefrom']));
The key things to note are the characters used in the first parameter.
j represents the day without leading zeros
n represents the month without leading zeros
There are many other options you have, just have a read through the documentation.
Please note that a simple search of 'PHP date' on Google would have found this solution for you
$adatefrom = date_create($_POST['datefrom']);
$adateto = date_create($_POST['adateto']);
$adatefrom = date_format($adatefrom, 'j/n/Y');
$adateto = date_format($adateto, 'j/n/Y');
you are welcome! ;)

PHP Odd time when adding

To get time I do this
$day_timmer = time()
echo $day_timmer;
This is what printsout 1332533147
But when I add to it I get this
$day_timmer = time() * 1000;
echo $day_timmer;
This is what printsout 1.332533387E+12
No clue what I'm doing wrong.
So I get voted down for wondering why letters are in my numbers lol
That's called scientific notation, and it's all right. E+12 stands for 1012 and in your case, this is exactly the given number:
1.332533387E+12 = 1.332533387 * 10^12 = 1,332,533,387,000
To fix your problem use the following statement.
<?php
echo date("h:i a");
?>

range() issuing a warning when date_diff is used

The following script issues a
'Warning: range() [function.range]: step exceeds the specified range in'
only when the date_diff function is called. Does anyone know why?
<?php
$array = array(
"Interno",
"id"
);
$step = count($array) - 1;
foreach (range(0, $step) as $number) {
echo '<p>'.$number.'</p>';
}
$datetime1 = new DateTime('2010-08-2');
$datetime2 = new DateTime('2009-07-30');
$interval = date_diff($datetime1,$datetime2);
?>
Well, the two functions have nothing to do with each other.
Secondly, the second parameter to range is not a step, it's a maximum value (see the range docs... So if you're getting a step exceeds the specified range error, I'd guess that the default step value 1 is larger than the max of the range (the result of count($array) - 1)... I'm not sure why that's happening in your code, but it's a start
That's a PHP bug.
Still present as of 5.3.5, it seems that was fixed in 5.3.6.
https://bugs.php.net/bug.php?id=51894
I do agree with ircmaxell, function range and date_diff are not related and do not interact in any way. The issue should be in your array which get altered in some way.
Also, as for me, your example contain unnecessary operations like count and range and it could be shortened to this:
<?php
$array = array(
"Interno",
"id"
);
foreach ($array as $number => $value) {
echo '<p>'.$number.'</p>';
}
$datetime1 = new DateTime('2010-08-2');
$datetime2 = new DateTime('2009-07-30');
$interval = date_diff($datetime1,$datetime2);
?>

Categories