strtotime conversion with variable error - php

I am getting wrong output i.e. 1194908400
None of this is working i.e. I tried to put double quotes around variable, tried without quotation marks. result is same and wrong.
$d='07-11-13';
echo $d;
echo strtotime($d);
echo "<br>";
echo strtotime("$d");

After a few checks, the error is actually NOT in the format you're passing, but rather on the way you're passing it.
What you should do is just replace "13" with "2013":
strtotime("07-11-2013");
output: 1383778800
echo strtotime("2013-11-07");
output: 1383778800
echo strtotime('07-11-13');
output: 1194908400

The problem is you need to specify a 4 digit year, so that strtime can fully work out which date format you are using. With 07-11-13 it probably thinks you are using 2007-11-13.
Change it to 07-11-2013 and you will get the correct answer.

$d='07-11-13' is wrong, Try this
$d='2013-11-13';
echo $d;
echo strtotime($d);
echo "<br>";
echo strtotime("$d")

strtotime() will accept:
m/d/y
d-m-y
With / it expects month/day and with - it expects day-month.

Related

How to format "datetime -local" value in php fetched from mysql?

I have an input field in html where I want to echo the value of datetime stored in mysql.
<input type="datetime -local" name="schedule" value="<?php echo $result['schedule']; ?>
But despite the value being there in mysql db as YY/mm/dd h:m:s the value is not showing up. What could be the possible issue? When I try to echo outside the input value it displays the result. My thoughts are that the datetime needs formatting according to the html datetime -local input field.
Please suggest me any way if possible.
T is a format character(Timezone abbreviation e.g: EST, MDT…) so you can't use it directly. Escape it (\T) to get a literal T character:
datetime.format.php
You can prevent a recognized character in the format string from being expanded by escaping it with a preceding backslash.
<?php
$date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));
?>
<input type="datetime-local" name="schedule" value="<?php echo $date; ?>"/>
use <?=$date?> instead of <?php echo $date; ?>
You are filling it the wrong way. Read the documentation about the input-type datetime-local The correct format is YYYY-MM-DDTHH:MM - note the T as divider between date and time.
Also:
you wrote the type wrong. Correct is datetime-local
you are missing the closing quote at the value attribute.
This is how to fix it
<input type="datetime-local" name="schedule" value="<?php echo $date = date("Y-m-d\TH:i:s", strtotime($result['schedule']));?>">

Error getting part of url separated by blackslash

I want to get last part of url separated by blackslash
Here is my code
<?php
$str = "C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx";
echo basename($str);
?>
I want output as this
10819_21098672691424384960_Copy_of_tristan_data_cleanup_version_FBL_3.2.xlsx
But I am getting output as this
Not sure why I am getting such output .
You need to convert " to ' and then it will work:-
<?php
$str = 'C:\xampp\htdocs\sanimailer\storage\mail-attachments\10819_21098672691424384960_C
opy_of_tristan_data_cleanup_version_FBL_3.2.xlsx';
echo basename($str);
?>
Output:- https://eval.in/829073
The problem you are facing because "\10" converted to that symbol
check here:- https://eval.in/829077

How to correctly send php variables through ng-click parameters

Currently i'm trying to send a time and a date from php to my angular controller by using a ng-click.
$time is a date
$today is a datetime
Can someone explain me why this works
<button ng-click="getClickInfo('<?php echo $time; ?>', '<?php echo $today; ?>')";</button>
But when i try to achieve the same result within an echo like this, it gives me different and incorrect output
<?php echo '<button ng-click="getClickInfo(' . $time . ', ' . $today . ')";></button>'; ?>
I tried to search on the internet for a solution but i couldn't really find a topic about it. Hope someone can explain me what is happening in this scenario. Redirection to any articles on this topic would be really helpfull for me.
Output:
01:00 // incorrect output
01/01/1970 // incorrect output
20.30 // desired output
22-04-2016 // desired output
You have to quote the javascript function arguments, otherwise you get unpredictable results
The ctach here is that the HTML ng-click attribute must be enclosed in double quotes and the attribute value (your function) must not contain double quotes, because it would break the HTML
Furthermore, the ; is not needed, you're putting it outside the ng-click HTML attribute value, that is not valid HTML
Here's a fix, note that quotes are escaped with backslash inside PHP strings:
<?php echo '<button ng-click="getClickInfo(\'' . $time . '\', \'' . $today . '\')"></button>' ?>
Here's a more readable way to do it, I would recommend this approach:
<?php echo '<button ng-click="getClickInfo(' . "'$time', '$today'" . ')";></button>' ?>

Regex - the difference in \\n and \n

Sorry to add another "Regex explanation" question to the internet but I must know the reason for this. I have ran this regex through RegexBuddy and Regex101.com with no help.
I came across the following regex ("%4d%[^\\n]") while debugging a time parsing function. Every now and then I would receive an 'invalid date' error but only during the months of January and June. I mocked up some code to recreate exactly what was happening but I can't figure out why removing the one slash fixes it.
<?php
$format = '%Y/%b/%d';
$random_date_strings = array(
'2015/Jan/03',
'1985/Feb/13',
'2001/Mar/25',
'1948/Apr/02',
'1948/May/19',
'2020/Jun/22',
'1867/Jul/09',
'1901/Aug/11',
'1945/Sep/21',
'2000/Oct/31',
'2009/Nov/24',
'2015/Dec/02'
);
$year = null;
$rest_of_string = null;
echo 'Bad Regex:';
echo '<br/><br/>';
foreach ($random_date_strings as $date_string) {
sscanf($date_string, "%4d%[^\\n]", $year, $rest_of_string);
print_data($date_string, $year, $rest_of_string);
}
echo 'Good Regex:';
echo '<br/><br/>';
foreach ($random_date_strings as $date_string) {
sscanf($date_string, "%4d%[^\n]", $year, $rest_of_string);
print_data($date_string, $year, $rest_of_string);
}
function print_data($d, $y, $r) {
echo 'Date string: ' . $d;
echo '<br/>';
echo 'Year: ' . $y;
echo '<br/>';
echo 'Rest of string: ' . $r;
echo '<br/>';
}
?>
Feel free to run this locally but the only two outputs I'm concerned about are the months of June and January. "%4d%[^\\n]" will truncate $rest_of_string to /Ju and /Ja while "%4d%[^\n]" displays the rest of the string as expected (/Jan/03 & /Jun/22).
Here's my interpretation of the faulty regex:
%4d% - Get four digits.
[^\\n] - Look for those digits in between the beginning of the string and a new line.
Can anyone please correct my explanation and/or tell me why removing the slash gives me the result I expect?
I don't care for the HOW...I need the WHY.
Like #LucasTrzesniewski pointed out, that's sscanf() syntax, it has nothing to do with Regex. The format is explained in the sprintf() page.
In your pattern "%4d%[^\\n]", the two \\ translate to a single backslash character. So the correct interpretation of the "faulty" pattern is:
%4d - Get four digits.
%[^\\n] - Look for all characters that are not a backslash or the letter "n"
That's why it matches everything up until the "n" in "Jan" and "Jun".
The correct pattern is "%4d%[^\n]", where the \n translates to a new line character, and it's interpretation is:
%4d - Get four digits.
%[^\n] - Look for all characters that are not a new line

Price formatting for Portuguese

On my website, echo $symbols['currency']; echo $fields['price']; holds a value in US currency format and it outputs R$19800
and echo $symbols['currency']; echo number_format($fields['price']); outputs R$19,800
How do I format it to output R$19.800,00 which is Portuguese price formatting?
I tried
echo $symbols['currency'];
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $fields['price']);
and it outputs R$EUR 19.800,00 which is corrent, but Im finding it hard to remove EUR from printing. Thanks a lot.
Simply use the PHP function str_replace:
$money = money_format('%.2n', $fields['price']);
echo str_replace("EUR", "", $money);
You can do this without locale and str_replace() (which I consider just fixing/hiding something done not correctly) by just using number_format():
echo $symbols['currency'];
echo number_format((float) $fields['price'], 2, ',', '.');

Categories