I have an array :
$multiple_dates[0] = 2013-06-09 00:00:00;
$multiple_dates[1] = 2014-06-13 12:23:00;
Here is my code :
$format = "d-m-Y h:m:s";
for ($i = 0; $i < count($multiple_dates); $i++) {
$date_display = date_format(date_create($multiple_dates[$i]), $format);
$fdate = date_translate($format, $date_display);
echo $fdate;
}
But output is :
09-06-2013 12:06:0013-06-2014 12:06:00
Time is not correct...Any idea ?
That's because minutes fraction is i, not m (which is month)
So
$format = "Y-m-d H:i:s";
Your format is invalid. Try with:
$format = "d-m-Y h:i:s";
It should be "i" like this
$format = "d-m-Y h:i:s";
edit like this you can get it
Correct format is
$format = "d-m-Y H:i:s";
Check this code --
<?php
$mydate = '2014-06-13 12:23:00';
$format = "d-m-Y h:i:s";
echo $date_display = date_format(date_create($mydate), $format);
go for $timestamp = strtotime("09-06-2013 12:06:00") and set on date('d-m-Y h:m:s', $timestamp);
for ($i = 0; $i < count($multiple_dates); $i++) {
$timestamp = strtotime($multiple_dates[$i]));
$fdate=date('d-m-Y h:m:s', $timestamp);
echo $fdate;
}
You can just use strtotime. E.g.:
foreach ($multiple_dates as $date) {
echo date($format, strtotime($date));
}
tryin this one is easier....
for ($i = 0; $i < count($multiple_dates); $i++) {
echo date('d-m-Y H:i:s',strtotime($multiple_dates[$i]));
}
i would prefer this
Related
I have the following line in a file that I need to replace 2 parts of. This is the original line:
'display min(min(sindex,lon=%lon1%,lon=%lon2%),lat=%lat1%,lat=%lat2%)'
I need to replace it with this:
'display amin(actualPrecip,lon=%lon1%,lon=%lon2%,lat=%lat1%,lat=%lat2%)'
I used regexr.com to generate this regex to match the 2 parts, but not sure what to do with it. Basically I need to use sed to do an inplace replacement.
('display min\(min)|(\)\,)
That generated this on regexr.com:
'display min(min(sindex,lon=%lon1%,lon=%lon2%),lat=%lat1%,lat=%lat2%)'
So the first part needs to be replaced with 'display amin( and 2nd match needs to be replaced with just a comma. Is there an easy way to do this using sed?
Cheers, Mike
I ended up doing it in PHP instead of a regular expression, it achieved the same results:
<?php
exec("ls -b *.gs", $result);
foreach($result as $filename) {
echo "filename {$filename} \n";
$data = file_get_contents($filename);
$start = stripos($data, "display min(min(", 1);
$replaced = false;
if ($start !== false) {
$finish = $start + 16;
$data = substr_replace($data, "display amin(", $start, $finish - $start);
$start = stripos($data, "),", $finish+1);
$finish = $start+2;
$data = substr_replace($data, ",", $start, $finish - $start);
$replaced = true;
}
$start = stripos($data, "display max(max(", 1);
if ($start !== false) {
$finish = $start + 16;
$data = substr_replace($data, "display amax(", $start, $finish - $start);
$start = stripos($data, "),", $finish+1);
$finish = $start+2;
$data = substr_replace($data, ",", $start, $finish - $start);
$replaced = true;
}
if ($replaced === true) file_put_contents($filename, $data);
}
?>
This might work for you (GNU sed):
sed 's/min(min(sindex\([^)]*\))/amin(actualPrecip\1/' file
Use pattern matching and a back reference to format as desired.
when i enter 55 i want the function to print the fibonacci sequence 55 times but the sequence stops at 55
function Get-Fibonacci ($n) {
$current = 0 ;
$previous = 1;
while ($current -lt $n) {
$current;
$current,$previous = ($current+$previous),$current
}
}
function Get-Fibonacci ($n) {
$current = 0 ;
$previous = 1;
for ($i=0; $i -lt $n; $i++) {
$current;
$current,$previous = ($current+$previous),$current
}
}
Here's an answer which doesn't rewrite using a for loop:
function Get-Fibonacci ($n) {
$current = 0 ;
$previous = 1;
$position=0
while ($position -lt $n) {
$position++
'{0} : {1}' -f $position,$current
$current,$previous = ($current+$previous),$current
}
}
This is supposed to put the contents of nums into decreasing order, however sort does not change the contents of nums. Many sites I read have said to pass by reference, but I don't think I'm dereferencing the argument correctly. Bear with me, this is my first Perl program :)
#! /usr/bin/env perl
sub sort {
my #arr = #_;
my $len = scalar #arr;
for (my $i = 1; $i < $len-1; $i = $i + 1) {
my $max = $i;
for (my $j = $i + 1; $j < $len; $j = $j + 1) {
if ($arr[$j] > $arr[$max]) {
$max = $j
}
}
$temp = $arr[$max];
$arr[$max] = $arr[$i];
$arr[$i] = $temp;
}
}
print "Enter 10 numbers: ";
my $numbers = <STDIN>;
my #nums = split ' ', $numbers;
print "Unsorted: #nums\n";
sort \#nums;
print "Sorted: #nums\n";
#Matt Jacob helped me out with this one. This is what I was looking for.
#! /usr/bin/env perl
sub selection_sort {
my $arr = shift;
my $len = scalar #$arr;
for (my $i = 0; $i < $len-1; $i++) {
my $max = $i;
for (my $j = $i + 1; $j < $len; $j++) {
if (#$arr[$j] > #$arr[$max]) {
$max = $j
}
}
my $temp = #$arr[$max];
#$arr[$max] = #$arr[$i];
#$arr[$i] = $temp;
}
}
print "Enter 10 numbers: ";
my $input = <STDIN>;
my #integers = split ' ', $input;
print "Unsorted: #integers\n";
selection_sort \#integers;
print "Sorted: #integers\n";
When ever I run this bit of code. it doesn't display any output. Anyone see anything wrong?
I am trying to display this in the out put:
A
AA
AAA
AAAB
AAABA
AAABAA
AAABAAA
AAABAAAB
etc.
#!/usr/local/bin/perl
$A = 3;
$B = 1;
$i = 1;
$output = "";
$j = 1;
while ($i <= $ARGV[0]) {
while ($j <= $i) {
if ($A == 0 && $B == 0) {
$A = 3;
$B = 1;
}
if ($A > 0) {
$output.= "A";
$A--;
}
else {
$output.= "B";
$B--;
}
$j++;
}
print($output . "\n");
$i++;
}
It works for me when I run it with a numeric argument (number of lines).
An idea how to simplify the code:
#!/usr/bin/perl
use warnings;
use strict;
my $count = shift;
my $A = 3;
my $B = 1;
my $string = q();
$string .= ('A' x $A) . ('B' x $B) while $count > length $string;
print substr($string, 0, $_), "\n" for 1 .. $count;
It uses a different algorithm - it creates the longest possible string, and then outputs parts of it.
if there is no #ARGV, while ($i <= $ARGV[0]) never runs.
#ARGV is an array of the command line arguments provided when the script is executed. you did not provide any command line arguments. if you had use warnings in effect, you would be warned that $ARGV[0] is uninitialized.
As from ikegami comment. You cann't pass the input at when the program is compile. For example, consider your file name is algo.pl. Can you run your program with
perl algo.pl 10
Here 10 is the input value of the program. In program value is retrieve by the $ARGV[0]
so in your program looks like while ($i <= $ARGV[0]).
If you want pass the several values like perl filename.pl 12 data1 data2In your data retrieve by $ARGV[0] $ARGV[1] $ARGV[2] for more information see here.
If you want pass the input at the time of execution used STDIN
use warnings;
use strict;
my $A = 3;
my $B = 1;
my $i = 1;
my $output = "";
my $j = 1;
print "Enter the value: ";
chomp(my $value = <STDIN>);
while ($i <= $value) {
while ($j <= $i) {
if ($A == 0 && $B == 0) {
$A = 3;
$B = 1;
}
if ($A > 0) {
$output.= "A";
$A--;
}
else {
$output.= "B";
$B--;
}
$j++;
}
print($output . "\n");
$i++;
}
I want to print some dates from a site with a structure like this:
<tr><td><b>(.*?)</b></td>
<td align=".*?"/date/(\d+)-(\d+)/">.*?</a> (\d+)</td>
<td>(.*?)*</td></tr>
etc.
my $country = $1;
my $month = $2;
my $day = $3;
my $year = $4;
my $event = $5;
I need to extract only those where the $country is 'USA' but if I use the while statement the code loops endlessly through the first match. How do I rework the script to extract each found USA date?
sub getSpec {
my $line = shift;
my $site = getSite($line);
while ($site =~ s/.../) {
my $country = $1;
my $month = $2;
my $day = $3;
my $year = $4;
my $event = $5;
if ($country =~ /USA/i) {
print $month.$date.$year.$country.$event."\n";
}
}
}
A global match should make it for you:
while ($site =~ m/.../g) {
For details, look in the documentation.
It looks like you're not changing the string after the first match. Try reading $site (it's html of the whole site, right?) line by line, so the loop would look like this
(my Perl is a little bit rusty, this is only rough sketch, sorry for that):
while ( $_ = another_line_from_$site)
{
if($_ =~ s/.../) {
{variables}
if($country =~ /USA/i)
{ other_stuff }
}
}