PHP Date Formatting - php

When I write this strftime("%b %d, %Y %l:%I %p" ,strtotime($runrows['date'])); it always displays incorrectly (as in it displays 10:10 or 1:01 or 4:04)
Im trying to diplay the date as July 4, 20ll 7:32 pm
How would I fix this?

can you post what you get from $runrows['date']?
http://php.net/manual/en/function.strftime.php
Here are few examples:
// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
echo "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/28/2002")) . "\n";
// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
echo "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/30/2002")) . "\n";
// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
echo "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . "\n";
// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
echo "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . "\n";

Related

group archive in php and mysql

I want to create an archive list like this:
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
2015
November 2015
testing 1 of 15
but it is showing like this:
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
2015
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
This is my source code:
$query = "SELECT * FROM blogs ORDER BY date DESC";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet)){
$newsArray = array();
echo '<ul>' . PHP_EOL;
while ($newsResult = mysql_fetch_array($resultSet)){
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$timePeriodY = date('Y',strtotime($timePeriod));
$timePeriodM = date('F',strtotime($timePeriod));
/*if (!isset($newsArray[$timePeriod])){
$newsArray[$timePeriod] = array();
}*/
$newsArray[$timePeriod][] = $newsResult;
}
//by year
foreach ($newsArray as $timePeriod => $newsItems){
$timePeriodY = date('Y',strtotime($timePeriod));
echo '<li><strong>' . $timePeriodY . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//by month
foreach ($newsArray as $timePeriod => $newsItems){
echo '<li><strong>' . $timePeriod . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//news items
foreach ($newsItems as $item){
echo '<li>';
echo ''.$item["titlename"].'';
echo '</li>' . PHP_EOL;
}
//end by month
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
//end by year
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
echo '<li> </li>' . PHP_EOL;
echo '</ul>' . PHP_EOL;
} else {
echo 'No Blog Found';
}
Please help me with this and thanks in Advance.
You're "grouping" by "F Y" when you should be "grouping" by "Y" and then "F Y"
For example :
$newsArray[$timePeriod][] = $newsResult;
Should be something like
$newsArray[$timePeriodY][timePeriod][]= $newsResult;
Your code will then become:
$query = "SELECT * FROM blogs ORDER BY date DESC";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet)){
$newsArray = array();
echo '<ul>' . PHP_EOL;
while ($newsResult = mysql_fetch_array($resultSet)){
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$timePeriodY = date('Y',strtotime($timePeriod));
$timePeriodM = date('F',strtotime($timePeriod));
$newsArray[$timePeriodY][$timePeriod][] = $newsResult;
}
//by year
foreach ($newsArray as $timePeriodY => $newsItems){
echo '<li><strong>' . $timePeriodY . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//by month
foreach ($newsItems as $timePeriod => $items){
echo '<li><strong>' . $timePeriod . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//news items
foreach ($items as $item){
echo '<li>';
echo ''.$item["titlename"].'';
echo '</li>' . PHP_EOL;
}
//end by month
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
//end by year
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
echo '<li> </li>' . PHP_EOL;
echo '</ul>' . PHP_EOL;
} else {
echo 'No Blog Found';
}

Using date variables in an api

I have created a page that asks the user for a date and outputs results from an api. The api requires a start date and end date that can be no more than 7days apart. I have set it up so a user can enter a date and the end date will automatically be set for 7 days later.
I am having issues using the date function, It appears now that the code will automatically use todays date before the user can input their choose.
I want the user to be able to choose there date whether it be todays or a future date, I want my api call to wait for users input but not sure how this can be done.
<?php
$startDate = date('Y-m-d', strtotime(isset($_GET['start'])? $_GET['start'] :date('Y-m-d')));
$endDate = date('Y-m-d', strtotime('+7 days', strtotime($startDate)));
if($startDate){
echo "$endDate";
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
echo "<h1>Near-Earth Object (NEO) Report between " . $params['start_date'] . " and " . $params['end_date'] . "</h1>";
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
}
?>
It is nearly at what you wanted to begin with. Just need to add an else to the if statement and update the start/end dates to return false when no date is entered. Note: I also moved the header above the if and added a tertiary condition to display the date if it is already entered so that it is always displayed.
<?php
$startDate = isset($_GET['start']) ? date('Y-m-d', strtotime($_GET['start'] )) : false;
$endDate = $startDate ? date('Y-m-d', strtotime('+7 days', strtotime($startDate))) : false;
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
echo '<h1>Near-Earth Object (NEO) Report',
( $startDate ? ' between ' . $params['start_date'] . ' and ' . $params['end_date'] . '</h1>' : '</h1>');
if($startDate) {
echo "$endDate";
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
} else {
?><form action="" method="GET">
<label for="startdate">Please enter a start date (end date will be 7 days after the start date):</label>
<input id="startdate" type="date" name="start" />
<input type="submit" />
</form><?php
}

In PHP, how do you get the text offset of a timezone

How do you get the timezone offset text? For example if I pass in:
America/New_York
...I would like to receive back:
-04:00
I created a function called getTimezoneOffsetText
<?php
function getTimezoneOffsetText($timezone){
date_default_timezone_set( "UTC" );
$daylight_savings_offset_in_seconds = timezone_offset_get(timezone_open($timezone), new DateTime());
$mod = $daylight_savings_offset_in_seconds/60 % 60;
$min = abs($mod);
return sprintf('%+03d', $daylight_savings_offset_in_seconds/60/60) . ':' . sprintf('%02d', $min);
}
$offset1 = 'America/New_York';
$offset2 = 'Asia/Kabul';
$offset3 = 'Asia/Kathmandu';
$offset4 = 'Israel';
$offset5 = 'Greenwich';
$offset6 = 'America/Caracas';
echo '<pre>';
echo $offset1 . ":\t" . getTimezoneOffsetText($offset1) . '<br/>';
echo $offset2 . ":\t\t" . getTimezoneOffsetText($offset2) . '<br/>';
echo $offset3 . ":\t\t" . getTimezoneOffsetText($offset3) . '<br/>';
echo $offset4 . ":\t\t\t" . getTimezoneOffsetText($offset4) . '<br/>';
echo $offset5 . ":\t\t" . getTimezoneOffsetText($offset5) . '<br/>';
echo $offset6 . ":\t" . getTimezoneOffsetText($offset6) . '<br/>';
echo '</pre>';
It generates this data:
America/Caracas: -04:30
Asia/Kabul: +04:30
Asia/Kathmandu: +05:45
Israel: +03:00
Greenwich: +00:00
America/New_York: -04:00
Note that it gets half hour and other "non-standard" timezones such as Caracas, Kabul and Kathmandu. I'm curious though if there is already a function that exists that accomplishes this.

PHP substring extrac and calulate

I'm using to show exif data of a picture.
this is the code
<?php
$filename = "http://www.rallyfun.net/images/20140921231511_img_7369.jpg";
$exif = exif_read_data($filename, 0, true);
echo "Exposure: " . $exif["EXIF"]["ExposureTime"] . " sec.<br />";
echo "F: " . $exif["EXIF"]["FNumber"] . "<br />";
echo "ISO: " . $exif["EXIF"]["ISOSpeedRatings"] . "<br />";
?>
this is the result:
Exposure: 1/6400 sec.
F: 63/10 sec.
ISO: 1000
the result is ok except for the aperture: it must be displayed as (in this case) 6.3 so i need to divide the first number (63) with the number after the "/" (10).
how?
U can use explode for this.
$tmp = explode('/', str_replace(' sec.', '', $exif["EXIF"]["FNumber"]));
echo "F: " . ($tmp[0]/$tmp[1]) . "sec<br />";
You can use this code to display aperture:
preg_match('/([0-9]+)\/([0-9]+)/',$exif["EXIF"]["FNumber"],$m);
if($m[1] && $m[2])
{
$newaperture = $m[1]/$m[2];
echo $newaperture;
}
Try the following :
$n = $exif["EXIF"]["FNumber"];
$whole = floor($n); // 63
$fraction = $n - $whole; // .10
$result=$whole/$fraction;

Command-line PHP truncating date("c")

Here's my PHP code for a shell-script:
#!/usr/bin/php -q
<?php
$user = get_current_user();
$line = date("c") . " - " . $user + "\r\n";
echo "---------------------\n";
echo "user => $user\n";
echo "---------------------\n";
echo "date('c') => " . date("c") . "\n";
echo "---------------------\n";
echo "date('Ymd') => " . date("Ymd") . "\n";
echo "---------------------\n";
echo "line => $line\n";
echo "---------------------\n";
echo date("c") . " - " . $user + "\n";
echo "---------------------\n";
echo date("c") . $user + "\n";
echo "---------------------\n";
echo date("c") . " - " . "\n";
echo "---------------------\n";
$ret = file_put_contents("/var/lib/foo/bar/test.txt", $line, FILE_APPEND);
echo "file_put_contents => $ret\n";
?>
When I run it, I get this output:
roffle:/var/lib/foo/bar # php Test.php
---------------------
user => wwwrun
---------------------
date('c') => 2014-07-27T16:39:34-04:00
---------------------
date('Ymd') => 20140727
---------------------
line => 2014
---------------------
2014---------------------
2014---------------------
2014-07-27T16:39:34-04:00 -
---------------------
file_put_contents => 4
roffle:/var/lib/foo/bar #
Why is $line truncated along with the first two calls to echo date("c") and why is the third call to date("c") okay?
Looks like you've just mixed up + with the . for concatenation in the lines in question.

Categories