This code with a couple of functions to modify a date works:
<?php
function date_up($input_date){
echo date("Y-m-d",strtotime('+1 day',strtotime($input_date)));
}
function date_up_friday($input_date){
echo date("Y-m-d",strtotime('+3 day',strtotime($input_date)));
}
function date_down($input_date){
return date("Y-m-d",strtotime('-1 day',strtotime($input_date)));
}
function date_down_monday($input_date){
echo date("Y-m-d",strtotime('-3 day',strtotime($input_date)));
}
echo 'Input date: ';echo $date='2017-09-25';
echo '<BR><BR>';
echo 'Down 1: ';
echo date_down($date);
echo '<BR>';
echo 'Down 3: ';date_down_monday($date);
echo '<BR>';
echo 'Up 1: ';date_up($date);
echo '<BR>';
echo 'Up 3: ';date_up_friday($date);
?>
I am trying to send the output from the function to another page when the hyperlink. New code shown below. But no date is sent in the hyperlink. I don't understand this and I apologise if their is a simple explanation. I am new to working with PHP.
Many thanks in advance !
echo 'Input date: ';echo $date='2017-09-25';
echo '<BR><BR>';
echo '<a href="Dev/Update_date_shortfall.php?date="';
echo date_down($date);
echo '">Down 1: </a>';
echo date_down($date);
echo '<BR>';
echo 'Down 3: ';date_down_monday($date);
echo '<BR>';
echo 'Up 1: ';date_up($date);
echo '<BR>';
echo 'Up 3: ';date_up_friday($date);
You need to send it like this:
echo 'Down 1: ';
And in Update_date_shortfall.php, you need to
echo $_GET['date'];
you can try this
echo 'Down 1: ';
working demo : https://ideone.com/bZNwMr
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
As you can see my date.of.birth format is Y-m-d, so I hope to change my D.O.B (tarikh lahir) format to d-m-Y,please help.
This is model : Search_model.php
<?php
class Search_model extends CI_Model {
public function get_results($search_term='default')
{
// Use the Active Record class for safer queries.
// $this->load->helper('share_function');
$this->db->select('pesakit.rn,pesakit.nama,pesakit.tarikhlahir,jantina.nama as jantina,agama.nama as agama,bangsa.nama as bangsa');
$this->db->from('lifeline.pesakit as pesakit');
$this->db->join('jantina','jantina.kod = pesakit.jantina');
$this->db->join('agama','agama.kod = pesakit.agama');
$this->db->join('bangsa','bangsa.kod = pesakit.bangsa');
$this->db->where('rn',alphaToNumber($search_term));
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
}
}
This is view : search_results.php
<div>
<?php
foreach ($results as $val)
{
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
echo 'Date.of.Birth : '; echo $val['tarikhlahir']; echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
Check this code. Hopefully, it will help you.
$dob = "2012-03-08";
echo date("d-m-Y", strtotime($dob));
//output date
08-03-2012
You need to create a function dateFormat in Helper.
function dateFormat($date, $format = 'd-M-Y'){
return date($format, strtotime($date));
}
We can call this :
<td><?php echo dateFormat($val['tarikhlahir']);?></td>
Using above function, date will be returned as its in default parameter.
While it can be changed where we will call this function.
For example :
<td><?php echo dateFormat($val['tarikhlahir'], 'm-d-Y');?></td>
Try not to use the semicolon to concatenate the strings.
Use "Dot" to do this.
echo 'Date.of.Birth : ' . date('d m Y', strtotime($val['tarikhlahir']) . "<br/>";
date('d-m-Y', strtotime($the_date_you_want_to_convert));
http://php.net/manual/en/function.strtotime.php
As per the #Alex answer.
As per my knowledge, there is no separate date function in CodeIgniter.
You can use PHP date function.
http://php.net/manual/en/function.date.php
http://php.net/manual/en/function.strtotime.php
$val['tarikhlahir']='1959-01-30';
$date=date('d-m-Y',strtotime($val['tarikhlahir']));
echo $date;
Please replace your the code as given below
<div>
<?php
foreach ($results as $val)
{
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
$originalDate = $val['tarikhlahir'];
$newDate = date("d-m-Y", strtotime($originalDate));
echo 'Date.of.Birth : '; echo $newDate; echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
This may help you..Thanks!
since nobody mentioned it - imho the safest way to do this is the DateTime::createFromFormat function.
Something like that should work
<div>
<?php
foreach ($results as $val)
{
$objDateTime = DateTime::createFromFormat('Y-m-d', $val['tarikhlahir']);
echo 'RN : '; echo numberToAlpha( $val['rn']); echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
echo 'Date.of.Birth : '; echo $objDateTime->format('d-m-Y'); echo "<br/>";
echo 'Age:' ; echo calculateCurrentAge ($val['tarikhlahir']); echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
}
?>
</div>
I need to do something like
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
Any help will be highly useful. I am stuck in this from the very long time but not able to acheive this. please help
Below is my PHP code that I need to assigned again to php variable (this variable will be passed to return of my ajax call)
<div class="photo-post">
<h4 class="h4-1">Some data...</h4>
<?php
$i=0;
$unique_seq="888094499";
$pic_counter=1;
foreach (glob("target/*".$unique_seq."*") as $filename)
{
$i++;
}
$total_pics=$i;
echo "<div id='post-photo-slider'>";
if($i>1)
{
echo "<a href='#' class='control_next_photo'>></a>";
echo "<a href='#' class='control_prev_photo'><</a>";
}
echo "<ul>";
foreach (glob("target/*".$unique_seq."*") as $filename)
{
echo "<li>";
echo "<div class='photos-slide-container'>";
echo "<img src='".$filename."' width='540px' height='225px'>";
echo "<span class='count-span-photos'>".$pic_counter." of ".$total_pics."</span>";
echo "</div>";
echo "</li>";
$pic_counter++;
}
if ($i==1)
{
echo "<li></li>";
}
echo "</ul>";
echo "</div><!-- post-photo-slider END -->";
echo "<hr class='hr4'>";
?>
</div><!-- photo end -->
you cannot do it in this way
$var = "Hello World ..the value is <?php echo 'XYZ' ?>";
when you return this string to your javascript php code cannot be execute on client side!!!!
why do not return do this simple way?
$var = "Hello World ..the value is " . "XYZ";
I have this code
$css=0;
<?php
echo "<div>";
function style(){
global $css;
echo $css;
}
echo "</div>";
$css =5 ; style();
?>
what i want to happen is this
<div>5</div>
and what really happens is
<div></div>5
I need to declare the value of the variable then go back and echo it in the same place it intended to be in, and I can't change the order due to many reasons in the code structure .
UPDATE :: after reading all the answers it seems i wasn't clear enough .
<html>
<?php $css=0; ?>
<div id="<?php echo $css ?>"></div>
<div id ="K"></div>
<?php
$css=5;
?>
</html>
this is the situation
1- i can not declare $css before the echo.
2- i can not reposition anything from this structure .
3- i can not put #K after the declaring of the $css .
what i really need a way to put the value inside the variable and let echo where it's .
any ideas ?
you can try this as well using js
<?php
$css=0;
echo "<div id='tt'>";
/*function style(){global $css; echo $css;}*/
echo "</div>";
$css =5 ;
echo "<script>";
echo "document.getElementById('tt').innerHTML = $css";
echo "</script>";
?>
Your function is run when you call it, and as it is called below the you get the expected 5.
To fix this:
echo "<div>";
style();
echo "</div>";
<?php
echo "<div>";
function style() {
global $css;
echo $css;
}
$css =5 ;
style();
echo "</div>";
?>
or simply:
<?php
function style() {
global $css;
echo $css;
}
$css = 5;
echo "<div>" . style() . "</div>";
?>
Previously, you well calling the function to echo out the variable after closing the div.
I have a mysql query that brings in a list of results, say 10, each with its own 'last edited' date, and was wondering if it was possible to display/echo/print ONLY the most recent of these dates using php?
Now I know I could do this with a mysql query, but I need to display all of the available results, but only 1 date, (the most recent).
Hope this makes sense. Any help greatly appreciated. S.
<?php
$availQuery=mysql_query("select * from isavailability");
$availResult=mysql_fetch_array($availQuery);
$date = $availResult['editDate'];
$newDate = date('D dS F Y',strtotime($date));
$time = date('G:i:s',strtotime($date));
echo '<p>This page was last updated on '.$newDate.' at '.$time.'</p>' . PHP_EOL;
while($availR=mysql_fetch_array($availQuery)) {
echo '<tr class="rows">'. PHP_EOL;
echo '<td><p>'.$availR['title'].'</p></td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==1) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==2) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '<td>'; if ($availR['availability']==3) { echo $tick; } else { echo $cross; } echo '</td>'. PHP_EOL;
echo '</tr>'. PHP_EOL;
}
?>
It can be done in a single query: SELECT isavailabilit.*, MAX(editDate) AS LastEditDate FROM isavailabilit;
Use it this way in the loop: $availR['LastEditDate']
If your query is ordered by your timestamp then you could just grab the first one - if I understand your question:
$myDate = $myData[0]['myDateColumn'];
foreach($myData as $row)
{
echo $myDate . ' ' . $row['someotherColumn'] . "\n";
}
or something similar? Presuming your results are in an array.
$hometime= $Twitter->get_statusesHome_timeline();
Fatal error: Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct() expects parameter 1 to be string
<?php
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'key.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
$oauthToken='xxxxxxxxxxxxxxxxxxxxxxx';
$oauthSecret='xxxxxxxxxxxxxxxxxxxxxxxxx';
// user switched pages and came back or got here directly, stilled logged in
$Twitter->setToken($oauthToken,$oauthSecret);
$user= $Twitter->get_accountVerify_credentials();
echo "<img src=\"{$user->profile_image_url}\">";
echo "{$user->name}";
$hometime= $Twitter->get_statusesHome_timeline();
$twitter_status = new SimpleXMLElement($hometime);
foreach($twitter_status->status as $status){
echo '<div class="twitter_status">';
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo ''.$user->name.': ';
}
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at"><strong>Posted at:</strong> '.$status->created_at.'</div>';
echo '</div>';
}
?>
My best guess is that you might have turned off Warnings (and Notices) and that the
$hometime= $Twitter->get_statusesHome_timeline();
call doesn't return any xml but "false" because it can't connect (or something).
Did you try printing $hometime after the call ?
$hometime should be an array of status objects.
Try
<?php
$hometimeline = $Twitter->get_statusesHome_timeline();
foreach($hometimeline as $status){
echo '<div class="twitter_status">';
foreach($status->user as $user){
echo '<img src="'.$user->profile_image_url.'" class="twitter_image">';
echo ''.$user->name.': ';
}
echo $status->text;
echo '<br/>';
echo '<div class="twitter_posted_at"><strong>Posted at:</strong> '.$status->created_at.'</div>';
echo '</div>';
}