小编Bar*_*rry的帖子

将DateTime转换为String PHP

我已经研究了很多关于如何将PHP DateTime对象转换为String的网站.我总是看到"String to DateTime"而不是"DateTime to String"

可以回显PHP DateTime,但我想用PHP字符串函数处理我的DateTime.

我的问题是如何从这种代码开始将PHP dateTime Object变为字符串:

$dts = new DateTime(); //this returns the current date time
echo strlen($dts);
Run Code Online (Sandbox Code Playgroud)

php datetime

204
推荐指数
4
解决办法
57万
查看次数

laravel 5.4上传图片

我在laravel 5.4中上传文件的控制器代码:

if ($request->hasFile('input_img')) {
    if($request->file('input_img')->isValid()) {
        try {
            $file = $request->file('input_img');
            $name = rand(11111, 99999) . '.' . $file->getClientOriginalExtension();
            $request->file('input_img')->move("fotoupload", $name);
        } catch (Illuminate\Filesystem\FileNotFoundException $e) {

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

图像已成功上传,但代码引发了异常:

MimeTypeGuesser.php第123行中的FileNotFoundException

该文件在我的代码中有任何错误,或者它是laravel 5.4中的错误,任何人都可以帮我解决问题吗?

我的观点代码:

<form enctype="multipart/form-data" method="post" action="{{url('admin/post/insert')}}">
    {{ csrf_field() }}
    <div class="form-group">
        <label for="imageInput">File input</label>
        <input data-preview="#preview" name="input_img" type="file" id="imageInput">
        <img class="col-sm-6" id="preview"  src="">
        <p class="help-block">Example block-level help text here.</p>
    </div>
    <div class="form-group">
        <label for="">submit</label>
        <input class="form-control" type="submit">
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

php laravel-5.4

28
推荐指数
5
解决办法
12万
查看次数

模拟所有DateTime实例用于测试目的的时间

我希望能够在DateTimePHPUnit或Behat Test的持续时间内为每个实例化实例设置时间.

我正在测试与时间相关的业务逻辑.例如,类中的方法仅返回过去或将来的事件.

如果可能的话,我不想这样做:

  1. 写一个包装器DateTime并使用它代替DateTime整个代码.这将涉及重写我当前的代码库.

  2. 每次运行测试/套件时动态生成数据集.

所以问题是:是否有可能覆盖DateTimes行为以始终在请求时提供特定时间?

php testing datetime phpunit behat

20
推荐指数
2
解决办法
1万
查看次数

生成随机IP地址

我想生成一些随机IP地址.但是evertime这个generateIPAddress函数返回0.0.0.0字符串作为ipAddress.但它应该每次都返回一些除0.0.0.0以外的随机ipAddress.任何建议为什么会发生?

private void callingGeoService() {
    int p1 = 255;
    int p2 = 0;
    int p3 = 0;
    int inc = 5;

    String ipAddress = generateIPAddress(p1, p2, p3);

    p3 += inc;
    if (p3 > 255) {
        p3 = 0;
        p2 += inc;
        if (p2 > 255) {
            p2 = 0;
            p1--;
            if (p1 <= 0) {
                p1 = 0;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是generateIPAddress方法

private String generateIPAddress(int p1, int p2, int p3) {

    StringBuilder sb = null;

    int b1 = (p1 …
Run Code Online (Sandbox Code Playgroud)

java ip subnet

15
推荐指数
2
解决办法
2万
查看次数

更新表并在Laravel 5 Migration中添加数据

我需要在我的laravel项目中添加一个新列,没问题,我用它Schema::table()来更新,没关系.现在我需要找出我在这个表上有多少记录并更新一些值.

我有桌子Warrants:

Schema::create('warrant_grants', function(Blueprint $table) {
    $table->increments('id');
    $table->integer('warrant_plan_id');
    $table->integer('shareholder_id');
});
Run Code Online (Sandbox Code Playgroud)

所以我使用新的迁移文件创建了新字段:

Schema::table('warrant_grants',function ($table) {
    $table->string('name',100);
});
Run Code Online (Sandbox Code Playgroud)

现在我需要name使用一些值更新表中的这个字段,例如,如果表有100条记录,那么我需要在每一行中插入值"Warrant-X",其中X是以1到100开头的数字.例:

Warrant-1,Warrant-2,.... Warrant-100.

我花了几个小时寻找一些方法来使用种子,但我没有找到.所以基本上我有两个问题:

  • 我可以在Laravel 5中使用Seeds来更新值,或者我可以插入它们吗?
  • 我可以在Seeds(或迁移)中创建一些SQL来为我做这个更新吗?

php postgresql laravel

14
推荐指数
4
解决办法
2万
查看次数

对并发集合线程的linq操作是否安全?

例如以下代码线程安全:

ConcurrentQueue<Guid> _queue = new ConcurrentQueue<Guid>();
while(true)
{
for(int y = 0; y < 3; y++)
{
    if(y % 3 == 0)
    {
    System.Threading.Tasks.Task.Run(() => _queue.Enqueue(Guid.NewGuid()));
    }
    else if (y % 3 == 1)
    {
    Guid x;
    System.Threading.Tasks.Task.Run(() => _queue.TryDequeue(out x));
    }
    else if(y % 3 == 2)
    {
    System.Threading.Tasks.Task.Run(() =>
    {
        if (_queue.Any(t => t == testGuid))
        {
        // Do something
        }
    });

    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:显然标题不够清晰,所以更新了代码示例以包含实际的多线程行为,是的,上面的代码只是多线程行为的一个示例.

c# linq concurrency

8
推荐指数
1
解决办法
3644
查看次数

删除DataObject默认排序

我有一个DataObject默认排序:

class Author extends DataObject {
    private static $db = array('Name' => 'Varchar');
    private static $default_sort = 'Name ASC';
}
Run Code Online (Sandbox Code Playgroud)

我想要Author::get()数据,但没有任何排序.

所以......

Author::create(array('Name' => 'DEF'))->write();
Author::create(array('Name' => 'ABC'))->write();
Run Code Online (Sandbox Code Playgroud)

这将使用排序输出ABC然后输出DEF,但如果排序被清除,我会期望DEF然后是ABC.

我已经尝试了Author::get()->sort(''),Author::get()->sort(null)但两个人再次返回ABC然后返回DEF.

注意我问的原因是我有一个复杂的查询(使用连接),这导致一个问题,如果我尝试设置排序,我会收到错误.

您的SQL语法有错误; 检查与MySQL服务器版本对应的手册,以便在第1行的"ORDER BY"_SortColumn0"ASC"附近使用正确的语法

所以解决方案,以及我想知道的是,如何清除特定的默认排序,Author::get()同时保持现有的默认排序DataObject

php orm silverstripe

6
推荐指数
1
解决办法
114
查看次数

使用 PHPSpreadsheet 打开受密码保护的 XLSX 文件

我正在尝试使用PHPSpreadsheet文档)打开受密码保护的 Excel 文件 (.xlsx )。我知道密码,但我找不到打开它的方法。

load()方法\PhpOffice\PhpSpreadsheet\Reader\Xlsx不能插入密码,当我尝试加载文件时,它会返回错误(当然)。

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load('hello world.xlsx');
$sheet = $spreadsheet->getActiveSheet();
echo $sheet->getCell('A1')->getValue() . "\n";
Run Code Online (Sandbox Code Playgroud)

这是错误

警告:ZipArchive::getFromName():第 311 行 /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中的 Zip 对象无效或未初始化 警告:ZipArchive::getFromName():无效或未初始化的 Zip 对象在 /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 第 313 行警告:为 /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中的 foreach() 提供的参数无效第 350 行警告:ZipArchive::getFromName():/PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中的 Zip 对象无效或未初始化,第 311 行警告:ZipArchive::getFromName():无效或313 行 /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 中未初始化的 Zip 对象警告:在 /PHPOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php 第 397 行中为 foreach() 提供的参数无效警告:ZipArchive::getFromName():/PHPOffice/vendor/phpoffice 中的 Zip 对象无效或未初始化/phpspreadsheet/src/PhpSpreadsheet/Reader/Xlsx.php on line 311 警告:ZipArchive::getFromName(): Invalid or uninitialized …

php phpoffice phpspreadsheet

6
推荐指数
1
解决办法
2343
查看次数

希腊语中的日期和时间

我目前正在使用一个网站来获取雅典的时间:

$d = new DateTime("now", new DateTimeZone("Europe/Athens"));
echo $d->format("l, d M Y");
Run Code Online (Sandbox Code Playgroud)

但我希望日期以希腊文和相同格式显示。

php datetime localization

5
推荐指数
2
解决办法
8110
查看次数

Laravel:门面真的会在调用方法时创建新对象吗?

我有一个演示类通常通过

$this->app->bind('demo', function() { return new Demo(); }

一个立面

受保护的静态函数 getFacadeAccessor() { return 'demo'; }

类本身看起来像这样

课堂演示 
    {

        私人 $value1;        
        私人 $value2;        

        公共函数 setVal1($value)
        {
            $this->value1 = $value;
        }

        公共函数 setVal2($value)
        {
            $this->value2 = $value;
        }

        公共函数 getVals()
        {
            返回 '​​Val 1:' 。$this->value1 。'瓦尔2:'。$this->value2;
        }   

    }

有人告诉我,如果我在这个类上使用 Facade,Laravel 将实例化该类的一个对象,然后调用该对象上的方法,例如:

$app->make['demo']->setVal1();     
Run Code Online (Sandbox Code Playgroud)

Butt 我测试了更多,发现这种非常奇怪(至少对我而言)的行为:

如果我做

演示::setVal1('13654');

演示::setVal2('随机字符串')

我不应该使用 Demo::getVals() 来检索我刚刚创建的值,对吗?由于每次使用外观方法时都会实例化一个新对象,一个对象如何检索另一个对象的属性?应该有三个不同的实例,但我仍然能够从其他实例中检索属性......

我认为这只有在将类与 App::singleton 绑定而不是通过 App::bind 绑定时才有可能?

php facade inversion-of-control laravel

5
推荐指数
1
解决办法
2991
查看次数