how to check if date and string exist in text file - php

I have a php function to log users:
if(isset($_POST['enter'])){
if($_POST['name'] != "" && trim($_POST['name'])!= "" ){
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
$text_message = "<div class='msgln'><span class='chat-time'>".date("F j, g:i A")."</span> <b class='user-name'>".$_SESSION['name']."</b> </div>";
file_put_contents("users.html", $text_message, FILE_APPEND | LOCK_EX);
It's working fine, just if someone is refreshing the page, the "file_put_contents" function will log again.
So i'm trying to avoid duplicates:
$chck = file_get_contents("users.html");
if (false === strpos($chck, date("F j, g").' '.$_SESSION['name']){
//file_put_contents
}
I want to check for date - only date and hour (not minutes) and the username. As i'm using 12h time, how can i check for PM and AM ?
Output:
January 18, 7:33 AM username
January 18, 7:41 AM username
January 18, 7:45 AM username
For simplifying: i want to check for "January 18," "7" "AM" "username".

The sequence date("F j, g").' '.$_SESSION['name'] cannot exists, because there are some characters between the date and the user name.
You could check use a simple regular expression like "January 18, 9:[0-9]{2} AM"
$date = date('F j, g:[0-9]{2} A');
$match = preg_match('~' . $date . '~', $text_message);
if (strpos($text_message, $_SESSION['name']) === false && !$match) {
echo "match not found";
// file_put_contents()
}
else {
echo "match found";
}

I think it is hard to do that since your date is string ( not stored in a database ), in less words you can't get the date String to compare if it is already inserted, because if you have for example these inputs in users.html:
<div class='msgln'><span class='chat-time'>January 18, 7:00 AM</span> <b class='user-name'>evara</b> </div>
<div class='msgln'><span class='chat-time'>January 18, 7:31 AM</span> <b class='user-name'>Know-nothing</b> </div>
and then you got an request to insert new user, so you need to check if the user already had been inserted with
username = "evara" && date = January 18, 7:01 AM
so then you can't compare them. but why not comparing if just username was already inserted ?
if (false === strpos($chck,$_SESSION['name']){
//file_put_contents
}

Related

How to validate a date in PHP when the format can vary

I am working on a site and have to validate a date from a form. i am using jquery picker to select the date, however, the user can change the selection manually therefore i want to validate for any possible format (January, 1 2000, 1 jan 2000 etc).
I have looked around and am having trouble finding a solution. I am trying using strtottime to turn the date to a stamp and I am then using checkdate. The validation with strtotime works fine if i enter for instance January 40, 2000, however, if i enter a date that is below or equal to 31 with any month strtotime moves it to the following month, for instance, february 31, 2017 turns to March 3, 2017.
This is making the checkdate function useless as the date passed to it is always valid. is there a solution to this?
$thisD = 'February 31, 2017';
if (strtotime($thisD) === false)
{
echo "<BR>DATE FORMAT NOT VALID <BR>";
}
else
{
echo "<BR>DATE FORMAT VALID <BR>";
$thisDStr = strtotime($thisD);
echo "Original date was $thisD, date now is " . date('F, j Y',$thisDStr). ' -> ';
if (checkdate(date('n',$thisDStr), date('j',$thisDStr), date('Y',$thisDStr)))
{
echo ' is valid <br>';
}
else
{
echo ' is NOT valid <br>';
}
}//end else

Retrieving Birth DATE data from MySQL DB

I'm trying to retrieve a date data from mysql DB. But it returns N/A even though its not null. But when I comment out the 2nd If(statement) it returns January 01, 1970 but the actual data is June 6, 1952. BusDateHired and DateOfBirth
are in the same table. The membership date is perfectly fine, it retrieve the actual data from database.
# GET MEMBERS INFO.
$meminfo_sql = sqlCLIENTINFO($clientid, $br_code);
$meminfo_query = mysql_query($meminfo_sql) or die("ERROR GETTING MEMBER'S DATA".mysql_error());
$meminfo_data = mysql_fetch_array($meminfo_query);
# SET variables for members data.
$fullname = $meminfo_data['LName'].", ".$meminfo_data['FName']." ".$meminfo_data['MName'];
$temp_dateHired= explode("-", $meminfo_data['BusDateHired']);
$temp_dateopened = explode("-", $meminfo_data['DateOpened']);
$temp_birthDate= explode("-", $meminfo_data['DateOfBirth']);
# Date of Birth
$meminfo_birthDate= "";
if($meminfo_data['DateOfBirth'] != "No Data" || $meminfo_data['DateOfBirth'] != null || !is_null($meminfo_data['DateOfBirth'])){
$meminfo_birthDate = date("F d, Y", mktime(0,0,0,$temp_birthDate[1],$temp_birthDate[2],$temp_birthDate[0])); # date format January 01, 2013
}
#end of Date of Birth
# Date of hired..
$meminfo_dateHired= "";
if($meminfo_data['BusDateHired'] != "No Data"){
$meminfo_dateHired = date("F d, Y", mktime(0,0,0,$temp_dateHired[1],$temp_dateHired[2],$temp_dateHired[0])); # date-format January 01, 2013
}
if (is_null($meminfo_data['BusDateHired'])) {
$meminfo_dateHired = 'N/A';
}
# end of Date of hired
$meminfo_birthDate= "";
if($meminfo_data['DateOfBirth'] != "No Data" || $meminfo_data['DateOfBirth'] != null || !is_null($meminfo_data['DateOfBirth'])){
$meminfo_birthDate = date("F d, Y", mktime(0,0,0,$temp_birthDate[1],$temp_birthDate[2],$temp_birthDate[0])); # date format January 01, 2013
}
I have tried your code online with demo date its working fine.
echo date("F d, Y",mktime(0,0,0,02,03,2015));
Output:
February 03, 2015
Online DemoClick Here

Convert month numbers to letters PHP

I have this code to echo some records from a mysql database:
<?php
$host = "localhost";
$gebruikersnaam = "Admin";
$wachtwoord = "Test123";
mysql_connect($host, $gebruikersnaam, $wachtwoord);
$database = "olf";
mysql_select_db($database);
$result = mysql_query("SELECT * FROM agenda WHERE datum ORDER BY datum ASC LIMIT 0, 3");
while($row = mysql_fetch_array($result))
{
?>
<ul class="dates">
<li>
<span class="date"><?php echo "" .substr($row['datum'],5,2). "";?></strong></span>
</li>
<?php } ?>
</ul>
In the database the format of the 'datum' (date) is YYYY-MM-DD, I echo just the part MM. So this is the format: 01 (January), 02 (February) etc. What I want:
If " .substr($row['datum'],5,2). " is equal to 01, then convert it to JAN
If " .substr($row['datum'],5,2). " is equal to 02, then convert it to FEB
If " .substr($row['datum'],5,2). " is equal to 03, then convert it to MAR
ETC..
How do I need to make this?
You shouldn't have to hassle with this yourself - let MySQL do the heavy lifting for you:
SELECT DATE_FORMAT (datum, '%b') AS month_abbreviation
FROM agenda
Use the date function to display the date in a specific format.
In this case, M displays the three first letters of the months name.
echo date("M", strtotime($row['datum']));
Convert the incoming information to a DateTime object, then you can apply all sorts of fancy formatting rules:
$date = \DateTime::createFromFormat( "Y-m-d", $row['datum'] );
echo $date->format( 'M' ); // for just month in text
echo $date->format( 'D dS of F Y' ); // for example, "Monday 6th of November 2014"
Check out the workings of \DateTime on the php website:
http://nl1.php.net/manual/en/datetime.createfromformat.php

Issues converting MySQL datetime with PHP's date function

I'm trying to use PHP to convert some of my database's datetime entries so that they appear with the full month name, day, and year: ex. July 08 2011
The entries in the database all have the correct dates and times, but when I try converting them, they show up in the correct format but the day becomes the month (08 shows up as August), the day shows up as the year (11), and the month shows up as the year (July shows up as 2007). So July 08 2011 gets converted to August 11, 2007.
Code:
$date2 = date('F j Y', strtotime($date));
Anyone know what might be up?
var_dump($date):
string(14) "07-07-11 01:32"
More code:
while($get_row = mysql_fetch_array($get_rs)) {
$gb_str2 .= $tableOpen;
$name = $get_row["Name"];
$email = $get_row["Email"];
$comment = $get_row["Comment"];
$date = $get_row["Date"];
if(!empty($name)) {
// If name exists and email exists, link name to email
if(!empty($email)) {
$name="by $name";
}
// If name does exist and email exists, link email to email
} elseif (!empty($email)) {
$name = "by $email";
// Else make name blank
} else {
$name = "";
}
// Append to string we'll print later on
$date2 = date('F j Y', strtotime($date));
$gb_str2 .= "$comment<hr>< posted on $date2 $name>".$tableClose."<br>";
}
For some reason,
$date = date('F jS Y', strtotime($get_row['date']));
did the trick. I still don't really understand what was going wrong.

get day from date in php

I have the code below which I got from the php: mktime manual just edited it to use my own variables but for the day I always get "1".
$requested_day = $_GET['day'];
$requested_month = $_GET['month'];
$requested_year = $_GET['year'];
if (!empty ($requested_day) || empty ($requested_month) || empty ($requested_year))
{
echo $requested_day. " ".$requested_month." ".$requested_year." is on day : ". date("1", mktime(0, 0, 0, $requested_month, $requested_day, $requested_year));
}
I enter the values and it is outputted as:
23 09 2010 is on day : 1
I think I am having a problem on the "1" I entered on the date, I don't quite understand what this is actually for?
Thanks
Your format string should have a lower case L not a number one.
date("l", mktime(...
The "l" in the format string says to show the day of the week.
Brace yourself:
1 != l (the latter is a lower-case L)

Categories