How to remove repeated day from mysql datetime using php - php

Following php scripts
<?php
echo date('D, F d ', strtotime($event->starttime)); echo $this->timestamp(strtotime($event->starttime));
?>
display result as,
Mon, March 21 Mon at 8:00 AM
Fri, March 25 Fri at 9:00 AM
etc
Unfortunately its showing day (Here, Mon and Fri) twice. Here how to remove the second day( ie, before string at).
I would like to show output as
Fri, March 25 at 9:00 AM
Any help please...

You don't need two echoes for this, one echo is enough. You can add the 'at' text yourself, if you prepend it with a backslash.
From the PHP Date function:
<?php
echo date('D, F d \a\t H:i A', strtotime($event->starttime));
?>

echo strftime('%a, %B %d at %I:%M %p', strtotime($event->starttime));

A lowercase d passed to date() should return only the day number... check out http://php.net/manual/en/function.date.php for a list of all the valid parameters.
Try removing the space following d?

Related

Printing dates as strings in SQL

I'm building a CMS system using PHP for the first time. Part of this project involves me printing out a date from a column in my database using a loop. This is what the loop currently looks like:
<td><?php echo $record['fromDate']; ?> - <?php echo $record['toDate']; ?> | <?php echo $record['location']; ?></td>
The date is printing out like this: 2022-03-03 - 2022-03-23
What can I do to print it out as follows: 03 March 2022 - 23 March 2022
To begin, this can be interpreted as PHP problem rather than SQL
You can use date_format() PHP function and format it with 'd F Y'while displaying with HTML
date_format(date_create('2000-01-01'), 'd F Y')
// 01 January 2000
As per docs
F - A full textual representation of a month, such as January or March
so your code would look like
<?php echo date_format(date_create($record['fromDate']), 'd F Y'); ?> - <?php echo date_format(date_create($record['toDate']), 'd F Y'); ?>
Word of Warning: If you're not telling your program exactly how to parse a date, it's guessing.
At some point you're going to feed a date like 2022-03-04 into it and get a result that you're not expecting.
$date_str = '2022-03-03';
$dt = DateTime::createFromFormat('Y-m-d', $date_str)->format('d F Y');
var_dump($dt);
Output:
string(13) "03 March 2022"
As seen in the accepted answer to this similar post:
$originalDate = $record['fromDate']; //or $record['toDate']
$newDate = date("j F Y", strtotime($originalDate));
And use $newDate in your echo statement.
j is the day of the month (without leading zeros), F is the month name, and Y is the year.
date() php

PHP Date and POSIX date

I have two dates (from a datatime picker) that I send using post in PHP to this same page (I use a hidden <input> in a form).
$(function(){
flatpickr('#calendar',{
mode: "range",
onChange: function(dates) {
if(dates.length>1){
$("#formdate .from").val(+dates[0]/1000);
$("#formdate .to").val((+dates[1])/1000);
$("#formdate").submit();
}
}
});
});
Those dates are POSIX date, for example 1490569200 is 2017-03-27 00:00:00.
You can see that I divide them by 1000; it's because I use them in MySQL so it needs to be in second and not in millisecond.
Here is my datetime picker :
When I valid this second date (here the 26th), it call the function above and refresh the page. If I do a echo of my POSIX date I got the following :
1488326400, 1490486400 that are respectively the 1st and the 26th of March (normal behavior). Now I use the following function in PHP to convert them in a readable format :
echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);
It displays me the following :
Wednesday 1st of March 2017
Sunday 26th of March 2017
So until here, everything works fine !
But now if I pick up a date equal or above the 27th of March, here it's what's happen :
I do an echo of my POSIX dates :
1490313600, 1490655600 that are respectively the 24th and 28th (so no problem).
But when I use PHP date for the readable format :
echo date('l jS \of F Y',$from);
echo date('l jS \of F Y',$to);
Friday 24th of March 2017
Monday 27th of March 2017
Indeed, every dates I pick equal or above the 27th will be displayed as the day before.
If I use echo date('l jS \of F Y',1490655600), same issue, it will give me the 27th and no the 28th.
What's wrong ?
As said in the comment, that was due to the winter/summer time.

Convert 2017-03-01 to March 1st 2017 format inPHP

How to Convert 2017-03-01 to March 1st 2017 format in PHP.
$mark_entry_last_date=$exam_dates['last_date'];
echo date('M j<\sup>S</\sup> Y', $mark_entry_last_date);
Above code outputs current date only but I need the date value for $mark_entry_last_date. How to get a result like March 1st 2017 for given date using escape characters. ?
date function expects second parameter to be timestamp (integer) not string, so you need to use strtotime:
echo date('M j<\s\up>S</\s\up> Y', strtotime($mark_entry_last_date));
Also you need to escape u for sup tag. Since u is format for displaying microseconds.
Try without tags and use strtotime()
echo date("F jS Y", strtotime("2017-03-01"));
// output March 1st 2017
For more http://php.net/manual/en/function.strtotime.php

PHP How To Get Values From Dates

Would be grateful for any assistance with this.
I have:
$value = gmdate("F d Y", getlastmod());
This is returning (for example):-
June 24 2016
My question is how to extract the dayofweek information out of $value?
My output needs to look like:
Friday, June 24, 2016
Is there a way to extract the dayofweek, Month, dayofmonth and year out of $value? If not, how can I get those values from 'getlastmod' ?
Many thanks
First you will need to use strtotime to convert your current $value to timestamp.
Then by using the date()function you can get the day of the week.
Example:
<?php
echo date("l", strtotime("June 24 2016")); // Output: Friday
Try this instead:
$value = gmdate("l, F d, Y", getlastmod());
Output:
Friday, June 24, 2016
$myDate = date_create(getlastmod());
echo $myDate->format('L, F d, Y');
This will print Friday, June 24, 2016
More on date/time formats here:
http://php.net/manual/en/function.date.php
From the PHP formatting guide for date (gmdate is the same as date except it is using GMT) you can use "l" to get the full text of the day of the week.
http://php.net/manual/en/function.date.php
So in your example I think the following should work to meet your output needs.
$value=gmdate("l, F d, Y",getlastmod());

gmmktime and mktime give me the same number

My PHP timezone is 'Asia/Tehran' so I would not expect gmmktime give me the same result as mktime.
php info:
Default timezone Asia/Tehran
However when I type in my php console:
echo date('jS F Y h:i:s A (T)', mktime());
3rd September 2013 11:23:10 AM (IRDT)
echo date('jS F Y h:i:s A (T)', gmmktime());
3rd September 2013 11:23:16 AM (IRDT)
While I expect 3.5 or 4.5 hours time difference. Am I making a mistake somewhere?
you need to give Argumente inside of gmmktime(). Your mistake is, understand first mktime() vs gmmktime(). see this link.
And The workaround is simple, use gmdate() function to display dates created with gmmktime().
<?php
$inputDate = gmmktime(0,0,0,2,7,2012);
echo gmdate("M d Y H:i:s O", $inputDate);
// Feb 07 2012 00:00:00 +0000
?>

Categories