displaying icon if date created and local date are the same php - php

<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (servermonth == newdateformat) {
?>
<span class="label label-default">New</span>
<?php
}
?>
basically it should show the "New " icon when the date is created today. they are both echo-ing the same date but the span is not showing up

Try using this:
if (strtotime(servermonth) === strtotime(newdateformat)) {
echo '<span class="label label-default">New</span>';
}

<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat =$createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($servermonth) == strtotime($newdateformat)) { ?>
<span class="label label-default">New</span>
<?php } ?>

It should be:
<?php
date_default_timezone_set('America/New York');
$servermonth = date('m/d/Y ', time());
$createdate = new DateTime($row['createdon']);
$newdateformat = $createdate->format('m/d/Y') ;
// echo $newdateformat;
// echo $servermonth;
if (strtotime($sservermonth) == strtotime($newdateformat)) {
echo '<span class="label label-default">New</span>'
}
?>

Related

Display date format as DD MM YYYY in Codeigniter [duplicate]

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>

codeigniter date and time does not convert correctly in view

Dates that I want to display don't display properly. This is my code:
$row->created_when = '2016-09-27 11:06:54'
$row->approve_when = '02:43:06 09-28-2016'
<?php if(isset($records)): foreach($records as $row): ?>
......
<?php echo $row->first_name;?> <?php echo $row->last_name;?>
....
<?php if($row->approve_when==""){ ?>
<?php echo date("M d, Y H:i A", strtotime($row->created_when)); ?>
<?php echo $row->status;?>
<?php }else{ ?>
<?php echo date("M d, Y H:i A", strtotime($row->approve_when)); ?>
<?php echo $row->status;?>
<?php } ?>
but what displays in the view:
created when : Sep 27, 2016 11:06 AM
approve when : Jan 01, 1970 02:00 AM
approve_when doesn't convert properly
$row->approve_when = '02:43:06 09-28-2016' this is wrong date format. for strtotime function it must be:
$row->approve_when = '2016-28-09 02:43:06'
Code for converting to proper date format:
$date = '02:43:06 09-28-2016';
$ar = explode(' ', $date);
$ar2 = explode('-', $ar[1]);
$properDate = $ar2[2] . '-' . $ar2[0] . '-' . $ar2[1] . ' ' . $ar[0];
echo date('Y-m-d H:i:s', strtotime($properDate));

How to add class in a string in PHP

I would like to put class inside this string :
<div class="date-custom-page">
<?php
$date = get_field('jour_de_levenement', false, false);
$time = strtotime($date);
?>
<?php $dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>"; ?>
<?php $unixtimestamp = strtotime(get_field('jour_de_levenement', false, false));
echo date_i18n($dateformatstring, $unixtimestamp); ?>
I have tried this but only the p is taken, not the class.
Can someone help me ?
Thanks a lot !
Just use this :
$dateformatstring = "<p class='date-event'>j</p><p class='month'>F Y</p>";

How to show records(php+mysql) with datetime attributes in a user friendly tree view html list?

I got some records with column 'id','time','catagory','content' saved in MySql database.Demo PHP(Actually I am using Yii, the data were provided by dataprovider.)
And I need to present the data in a user-friendly way.
The list were group by "Week"=>"Date"=>"record". Demo Output html
Could some give me an smart way to do the php loop?
Should I need to do an Week loop to check the exist week and saved an key-value array,
then to check the any record were on the same day and saved in another array,
then finally do the output loop to show the html?Many thanks.
<div class="tree">
<ul>
<li>
<span>2014, Week 2 , 2014-01-05~2014-01-11</span>
<ul>
<li>
<span>Monday, January 6</span>
<ul>
<li>
<div class="record">
<span>08:00:30</span><br>
<span>Play</span><br>
<span>Do something good...</span><br>
</div>
</li>
</ul>
</li>
This is the php code:
<?php
//fake data setting
$recordlist=Array();
$record=Array();
$record['time']='2014-01-06 08:00:30';
$record['catagory']='Play';
$record['content']='Do something good...';
$recordlist[]=$record;
$record=Array();
$record['time']='2014-01-07 10:00:30';
$record['catagory']='Study';
$record['content']='Do something fun...';
$recordlist[]=$record;
$record=Array();
$record['time']='2014-01-07 12:00:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;
$record=Array();
$record['time']='2014-12-15 12:00:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;
$record=Array();
$record['time']='2014-12-12 12:01:30';
$record['catagory']='Sport';
$record['content']='Bike';
$recordlist[]=$record;
?>
<?php
//output
//how to output just as http://jsfiddle.net/euj89/
// Refactor Array
foreach($recordlist as $record){
$date = explode(" ",$record['time']);
$hour = $date[1];
$date = $date[0];
$week = date("W",strtotime($date));
$month = date("F",strtotime($date));
$year = date("Y",strtotime($date));
$newRecordList[$year][$month][$week][$date][$hour]['time'] = $record['time'];
$newRecordList[$year][$month][$week][$date][$hour]['category'] = $record['catagory'];
$newRecordList[$year][$month][$week][$date][$hour]['content'] = $record['content'];
}
//OUTPUT
?><div class="tree"><ul><?
foreach ($newRecordList as $year => $recordByYear) {
foreach ($recordByYear as $month => $recordByMonth) {
foreach ($recordByMonth as $week => $recordByWeek) {
?><li><span><?=$year?>, Week <?=$week?> , date</span><ul><?
foreach ($recordByWeek as $day => $recordByDay) {
?><li><span><?=date("l",strtotime($day))?>, <?=$month?> <?=$day?> </span><ul><?
foreach ($recordByDay as $hour => $recordByHour) {
?><li><div class="record">
<span><?=$hour?></span><br>
<span><?=$recordByHour['category']?></span><br>
<span><?=$recordByHour['content']?></span><br>
</div></li><?
}
?></ul></li><?
}
?></ul></li><?
}
}
}
?></ul></div><?
Output should be like this:
function getRecords($result){
$arr = new array();
while ($row = myqsli_fetch_array($result)) {
$arr[] = $row;
}
return $arr;
}
$week;
$date;
$records = getReceords($queryresultfromyourdb);
foreach($records as $rec)
{
if ($week != $rec["Week"])
{
echo $rec["Week"] . '<br>';
$week = $rec["Week"];
}
if ($date != $rec["Date"])
{
echo $rec["Date"] . '<br>';
$date = $rec["Date"];
}
echo $rec["record"] . '<br>';
}

Display last date a post was updated on WordPress

I'm using some PHP to display the last time a blog post was updated on WordPress using the get_the_time and get_the_modified_time functions. However, I can't get the last modified time to display inline in the paragraph.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
Here's a screenshot of the result:
the_modified_time prints the last modification time, so it prints it before you print your <p>.
Instead you'll want to use get_the_modified_time for setting $t, like so:
$t = get_the_modified_time('F d, Y');
It's probably because these functions don't return the result but echo it directly. Thus you must call them at the spot you need them to be.
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
echo "<p class=\"lastupdated\">Updated on ".the_modified_time('F d, Y')."</p>";
}
?>
You can also display the last modified time of a post through this code by placing it anywhere in your loop.
Posted on <?php the_time('F jS, Y') ?>
<?php $u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if($u_modified_time != $u_time) {
echo "and last modified on ";
the_modified_time('F jS, Y');
echo ". ";
} ?>
This is the complete code based on the above comments. Now it works and you just need to add this to your post template: Snippet (1)
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
For example, suppose that your post template is called content-post.php. Look for a part that looks like the below: Snippet (2)
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php endif;
Insert snippet (2) immediately before the closing tag of snippet (1) as follows:
<?php
// Post date
if ( in_array( 'post-date', $post_meta_top ) ) : ?>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php
$x = get_the_time('U');
$m = get_the_modified_time('U');
if ($m != $x) {
$t = get_the_modified_time('F d, Y');
echo "<p class=\"lastupdated\">Updated on $t </p>";
}
?>
<?php endif;
Now, you will get the original post date and the updated post date. Special thanks go to #Sebastian Paaske Tørholm for his comment.

Categories